一、两种注入属性方式
(1)set方法。
(2)有参构造函数。
二、使用方法
(1)在bean标签内部使用constructor-arg
标签可以设置构造函数的参数。
(2)在bean标签内部使用property
标签可以在创建对象后给该对象设置属性。
三、具体实现
(1)Maven添加依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.2.12.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.12.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>5.2.12.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.2.12.RELEASE</version>
</dependency>
(2)创建类
public class User {
private Long id;
private String name;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public User() {
}
public User(Long id) {
this.id = id;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
(3)编写配置文件spring-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="user" class="com.wsh.pojo.User">
<constructor-arg name="id" value="1"></constructor-arg>
<property name="name" value="李四"></property>
</bean>
</beans>
(4)JAVA程序
public void test(){
ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
User user = context.getBean("user", User.class);
System.out.println(user.toString());
}
输出
User{id=1, name='李四'}
四、注入外部Bean
(1)创建类
Department类
public class Department {
private Long id;
private String name;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Department() {
}
public Department(Long id, String name) {
this.id = id;
this.name = name;
}
@Override
public String toString() {
return "Department{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
Employee类
private Long id;
private String name;
private Department department;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Department getDepartment() {
return department;
}
public void setDepartment(Department department) {
this.department = department;
}
public Employee() {
}
public Employee(Long id, String name, Department department) {
this.id = id;
this.name = name;
this.department = department;
}
@Override
public String toString() {
return "Employee{" +
"id=" + id +
", name='" + name + '\'' +
", department=" + department +
'}';
}
}
(2)编写配置文件spring-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="department" class="com.wsh.pojo.Department">
<property name="id" value="1"></property>
<property name="name" value="研发部"></property>
</bean>
<bean id="employee" class="com.wsh.pojo.Employee">
<property name="id" value="1"></property>
<property name="name" value="张三"></property>
<property name="department" ref="department"></property>
</bean>
</beans>
(3)JAVA程序
public void test(){
ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
Employee employee = context.getBean("employee", Employee.class);
System.out.println(employee.toString());
}
输出
Employee{id=1, name='张三', department=Department{id=1, name='研发部'}}
五、注入集合类型
(1)创建类
public class Demo {
private List<Long> list;
private Map<Long, String> map;
private Long[] array;
private Set<Long> set;
public List<Long> getList() {
return list;
}
public void setList(List<Long> list) {
this.list = list;
}
public Map<Long, String> getMap() {
return map;
}
public void setMap(Map<Long, String> map) {
this.map = map;
}
public Long[] getArray() {
return array;
}
public void setArray(Long[] array) {
this.array = array;
}
public Set<Long> getSet() {
return set;
}
public void setSet(Set<Long> set) {
this.set = set;
}
public Demo() {
}
public Demo(List<Long> list, Map<Long, String> map, Long[] array, Set<Long> set) {
this.list = list;
this.map = map;
this.array = array;
this.set = set;
}
@Override
public String toString() {
return "Test{" +
"list=" + list +
", map=" + map +
", array=" + Arrays.toString(array) +
", set=" + set +
'}';
}
}
(2)编写配置文件spring-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="demo" class="com.wsh.pojo.Demo">
<property name="array">
<array>
<value>1</value>
<value>2</value>
</array>
</property>
<property name="list">
<list>
<value>1</value>
<value>2</value>
</list>
</property>
<property name="map">
<map>
<entry key="1" value="JAVA"></entry>
<entry key="2" value="C++"></entry>
</map>
</property>
<property name="set">
<set>
<value>1</value>
<value>2</value>
</set>
</property>
</bean>
</beans>
(3)JAVA程序
public void test(){
ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
Demo demo = context.getBean("demo", Demo.class);
System.out.println(demo.toString());
}
输出
Test{list=[1, 2], map={1=JAVA, 2=C++}, array=[1, 2], set=[1, 2]}
六、将Bean注入集合类型中
(1)创建类
public class Demo {
private List<Department> departments;
public List<Department> getDepartments() {
return departments;
}
public void setDepartments(List<Department> departments) {
this.departments = departments;
}
public Demo() {
}
public Demo(List<Department> departments) {
this.departments = departments;
}
@Override
public String toString() {
return "Demo{" +
"departments=" + departments +
'}';
}
}
(2)编写配置文件spring-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="demo" class="com.wsh.pojo.Demo">
<property name="departments">
<list>
<ref bean="department1"></ref>
<ref bean="department2"></ref>
</list>
</property>
</bean>
<bean id="department1" class="com.wsh.pojo.Department">
<property name="id" value="1"></property>
<property name="name" value="研发部"></property>
</bean>
<bean id="department2" class="com.wsh.pojo.Department">
<property name="id" value="2"></property>
<property name="name" value="销售部"></property>
</bean>
</beans>
(3)JAVA程序
public void test(){
ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
Demo demo = context.getBean("demo", Demo.class);
System.out.println(demo.toString());
}
输出
Demo{departments=[Department{id=1, name='研发部'}, Department{id=2, name='销售部'}]}
七、补充
(1)创建类
Demo类
@Data
@ToString
@AllArgsConstructor
@NoArgsConstructor
public class Demo {
private Long id;
private String name;
}
Tmp类
@Data
@ToString
@AllArgsConstructor
@NoArgsConstructor
public class Tmp {
private Demo demo;
}
(2)编写配置文件spring-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="tmp" class="com.wsh.pojo.Tmp">
<property name="demo">
<bean class="com.wsh.pojo.Demo">
<property name="id" value="1"></property>
</bean>
</property>
</bean>
</beans>
(3)JAVA程序
public void test(){
ApplicationContext context = new FileSystemXmlApplicationContext("classpath:spring-config.xml");
Tmp tmp = context.getBean("tmp", Tmp.class);
System.out.println(tmp.toString());
}
输出
Tmp(demo=Demo(id=1, name=null))