1.方法注入属性
(1)类
package com.leo.spring5;
public class Book {
private String name;
private String author;
public void setName(String name) {
this.name = name;
}
public void setAuthor(String author) {
this.author = author;
}
public void testDemo() {
System.out.println("name:" + name + ", author:" + author);
}
}
(2)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"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--set 方法注入属性-->
<bean id="book" class = "com.leo.spring5.Book">
<!--<property name="name" value="天龙八部"></property>-->
<!--属性中特殊字符处理,CDATA-->
<property name="name">
<value><![CDATA[《天龙八部》]]></value>
</property>
<property name="author" value="金庸"></property>
</bean>
</beans>
(3)测试
package com.leo.spring5.testdemo;
import com.leo.spring5.Book;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestSpring5 {
@Test
public void testBook1() {
ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
Book book = context.getBean("book", Book.class);
System.out.println(book);
book.testDemo();
}
}
(4)执行
2.构造参数注入属性
(1)类
package com.leo.spring5;
public class Orders {
private String name;
private String address;
public Orders(String name, String address) {
this.name = name;
this.address = address;
}
public void testDemo() {
System.out.println("name:" + name + ", address: " + address);
}
}
(2)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"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--有参数构造注入属性-->
<bean id ="orders" class = "com.leo.spring5.Orders">
<constructor-arg name="name" value="order name"></constructor-arg>
<constructor-arg name="address" value="china"></constructor-arg>
</bean>
</beans>
(3)测试
package com.leo.spring5.testdemo;
import com.leo.spring5.Orders;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestSpring5 {
@Test
public void testOrders() {
ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
Orders orders = context.getBean("orders", Orders.class);
System.out.println(orders);
orders.testDemo();
}
}
(4)执行
3.外部bean
(1)创建dao类
package com.leo.spring5.dao;
public interface UserDao {
public void update();
}
package com.leo.spring5.dao;
public class UserDaoImpl implements UserDao{
@Override
public void update() {
System.out.println("dao update");
}
}
(2)在service调用dao里面的方法
package com.leo.spring5.service;
import com.leo.spring5.dao.UserDao;
public class UserService {
private UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public void add() {
System.out.println("service add");
userDao.update();
}
}
(3)在spring配置文件中进行配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--service和dao对象创建-->
<bean id="userService" class="com.leo.spring5.service.UserService">
<!--注入userDao对象
name:为类里面属性名称
ref: 创建userDao对象bean标签的id值
-->
<property name="userDao" ref="userDaoImpl"></property>
</bean>
<bean id="userDaoImpl" class="com.leo.spring5.dao.UserDaoImpl"></bean>
</beans>
(4)测试
package com.leo.spring5.testdemo;
import com.leo.spring5.service.UserService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestBean {
@Test
public void testAdd() {
// 1.加载spring配置文件
ApplicationContext context = new ClassPathXmlApplicationContext("bean2.xml");
// 2.获取配置创建的对象
UserService userService = context.getBean("userService", UserService.class);
System.out.println(userService);
userService.add();
}
}
(5)执行
4.内部Bean和级联赋值
一对多的关系:部门跟员工
(1)类
package com.leo.spring5.bean;
public class Dept {
private String name;
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Dept{" +
"name='" + name + '\'' +
'}';
}
}
package com.leo.spring5.bean;
// 员工类
public class Emp {
private String name;
private String gender;
// 员工属于某一部门,使用对象进行表示
private Dept dept;
public void setName(String name) {
this.name = name;
}
public void setGender(String gender) {
this.gender = gender;
}
public void setDept(Dept dept) {
this.dept = dept;
}
public void add() {
System.out.println("name:" + name + ", dept:" + dept);
}
}
(2)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"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="emp" class="com.leo.spring5.bean.Emp">
<!--设置两个普通属性-->
<property name="name" value="leo"></property>
<property name="gender" value="male"></property>
<!--设置对象类型属性-->
<property name="dept">
<bean id="dept" class="com.leo.spring5.bean.Dept">
<property name="name" value="IT Department"></property>
</bean>
</property>
</bean>
</beans>
或
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="emp" class="com.leo.spring5.bean.Emp">
<!--设置两个普通属性-->
<property name="name" value="leo"></property>
<property name="gender" value="male"></property>
<!--级联赋值-->
<property name="dept" ref="dept"></property>
</bean>
<bean id="dept" class="com.leo.spring5.bean.Dept">
<property name="name" value="研发部"></property>
</bean>
</beans>
(3)测试
package com.leo.spring5.testdemo;
import com.leo.spring5.bean.Emp;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestBean {
@Test
public void testBean() {
ApplicationContext context = new ClassPathXmlApplicationContext("bean3.xml");
Emp emp = context.getBean("emp", Emp.class);
emp.add();
}
}
package com.leo.spring5.testdemo;
import com.leo.spring5.bean.Emp;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestBean {
@Test
public void testBean() {
ApplicationContext context = new ClassPathXmlApplicationContext("bean4.xml");
Emp emp = context.getBean("emp", Emp.class);
emp.add();
}
}