Bean的装配方式
一、基于XML的装配
(1)构造注入方式装配
User类:
public class User { private String username; private Integer password; private List<String> list; public User(){ } public User(String username, Integer password, List<String> list) { this.username = username; this.password = password; this.list = list; } @Override public String toString() { return "User{" + "username='" + username + '\'' + ", password=" + password + ", list=" + list + '}'; } }
Test类:
public class Test { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("com/chauncey/ioc/beans.xml"); User user = (User) context.getBean("userOne"); System.out.println(user); } }
beans.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-4.3.xsd"> <bean id="userOne" class="com.chauncey.ioc.User"> <constructor-arg index="0" value="小白"/> <constructor-arg index="1" value="123456"/> <constructor-arg index="2"> <list> <value>"我是构造注入方式"</value> <value>"我是构造注入方式"</value> </list> </constructor-arg> </bean> </beans>
运行结果:
(2)设值注入方式装配
User类:
public class User { private String username; private Integer password; private List<String> list; public User(){ } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public Integer getPassword() { return password; } public void setPassword(Integer password) { this.password = password; } public List<String> getList() { return list; } public void setList(List<String> list) { this.list = list; } @Override public String toString() { return "User{" + "username='" + username + '\'' + ", password=" + password + ", list=" + list + '}'; } }
beans.xml:
<bean id="userTwo" class="com.chauncey.ioc.User">
<property name="username" value="小灰"/>
<property name="password" value="123456"/>
<property name="list">
<list>
<value>"我是设值注入方式"</value>
<value>"我是设值注入方式"</value>
</list>
</property>
</bean>
运行结果:
二、基于注解
创建UserDao接口和UserService接口,在两个中添加save方法,创建两个接口的实现类UserDaoImpl和UserServiceImpl
UserDaoImpl接口:
@Repository("userDao") public class UserDaoImpl implements UserDao { @Override public void save() { System.out.println("UserDao...Save..."); } }
UserServiceImpl:
@Service("userService") public class UserServiceImpl implements UserService { @Resource(name = "userDao") private UserDao userDao; @Override public void save() { userDao.save(); System.out.println("UserService...Save..."); } }
UserController:
@Controller("userController") public class UserController { @Resource(name = "userService") private UserService userService; public void save() { userService.save(); System.out.println("Controller...Save..."); } }
Test类:
public class Test { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("com/chauncey/user/bean.xml"); UserController controller = (UserController) context.getBean("userController"); controller.save(); } }
bean.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:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!--对包下的bean进行扫描--> <context:component-scan base-package="com.chauncey.user"/> </beans>
运行结果:
三、自动装配
以注解的案例,在UserServiceImpl中添加setUserDao方法,在UserController中添加setUserService方法
bean.xml:
<bean id="userDao" class="com.chauncey.user.UserDaoImpl"/> <bean id="userService" class="com.chauncey.user.UserServiceImpl" autowire="byName"/> <bean id="userController" class="com.chauncey.user.UserController" autowire="byName"/>