User
@ToString
@Getter
public class User {
private Integer id;
private String name;
private Car car;
private List<String> products;
private Map<String, String> map;
private List<Car> cars;
//private Map<String, Car> mapCar;
//private Map<String, List<Car>> mapListCar;
public User(Integer id, String name, Car car) {
System.out.println("user 有参构造");
this.id = id;
this.name = name;
this.car = car;
}
public User() {
System.out.println("user 无参构造");
}
public void setId(Integer id) {
System.out.println("user setId");
this.id = id;
}
public void setName(String name) {
System.out.println("user setName");
this.name = name;
}
public void setCar(Car car) {
System.out.println("user setCar");
this.car = car;
}
public void setProducts(List<String> products) {
this.products = products;
}
public void setCars(List<Car> cars) {
this.cars = cars;
}
public void setMap(Map<String, String> map) {
this.map = map;
}
}
1. 有参构造
applicationContext.xml
constructor-arg 标签五个属性
index:从0开始,对应构造函数中参数顺序
name:参数名字
value:赋值
ref:当属性是对象时,值为对应该类的bean id
type:可以不写,自动识别
<bean id="user" class="net.wanho.pojo.User">
<constructor-arg name="id" value="11"/>
<constructor-arg name="name" value="张三"/>
<constructor-arg name="car" ref="car" type="net.wanho.pojo.Car"/>
</bean>
<bean id="car" class="net.wanho.pojo.Car">
<constructor-arg name="id" value="1"/>
<constructor-arg name="name" value="大G"/>
</bean>
测试
@Test
public void constructorArgs() {
ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
User user = ac.getBean("user", User.class);
System.out.println(user);
}
user 有参构造
User(id=11, name=张三, car=Car{id=1, name='大G'})
2. 无参构造和set方法
applicationContext.xml
property 标签4个属性
name:setXXX方法 将set去掉,并且首字母小写 比如setId 写作id
value:给简单类型赋值
ref:给引用类型赋值,值为该类在spring容器中的bean id
type:可以不写,自动识别
<bean id="user" class="net.wanho.pojo.User">
<!-- 简单类型 -->
<property name="id" value="11"/>
<property name="name" value="张三"/>
<!-- 单个引用 指向外部 -->
<property name="car" ref="car" />
<!--
简单类型集合 相同类型标签可以通用
单个元素:list set array
键值对:map props
-->
<property name="products">
<list>
<value>苹果</value>
<value>华为</value>
<value>三星</value>
</list>
</property>
<!-- Map<String, String> -->
<property name="map">
<map>
<entry key="1" value="a"/>
<entry key="2" value="b"/>
<entry>
<key>
<value>3</value>
</key>
<value>c</value>
</entry>
</map>
</property>
<!--
泛型为引用的集合
ref 引用外部创建好的bean
bean 新建一个bean
-->
<property name="cars">
<list>
<ref bean="car"/>
<ref bean="car"/>
<bean class="net.wanho.pojo.Car">
<property name="id" value="3"/>
<property name="name" value="奥迪"/>
</bean>
</list>
</property>
</bean>
<bean id="car" class="net.wanho.pojo.Car">
<property name="id" value="2"/>
<property name="name" value="大鳄"/>
</bean>
测试
@Test
public void constructorArgs() {
ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
User user = ac.getBean("user", User.class);
System.out.println(user);
}
user 无参构造
car 无参构造
car setId
car setName
car 无参构造
car setId
car setName
user setId
user setName
user setCar
User(id=11, name=张三, car=Car{id=2, name='大鳄'}, products=[苹果, 华为, 三星], map={1=a, 2=b, 3=c}, cars=[Car{id=2, name='大鳄'}, Car{id=2, name='大鳄'}, Car{id=3, name='奥迪'}])
3. p名称空间
applicationContext.xml
p名称空间 p:属性名="值"
本质上还是使用property标签 set方法注入
使注入对象类型,p:属性名-ref="id"
p名称空间不建议使用,就使用property标签
<bean id="employee1" class="net.wanho.pojo.Employee" p:id="1002" p:name="李四" p:menu-ref="menu"/>
<bean id="menu" class="net.wanho.pojo.Menu">
<property name="id" value="112"/>
<property name="name" value="职位管理"/>
</bean>