一.对象的依赖关系:
Spring中,如何给对象的属性赋值? 【DI, 依赖注入】
1) 通过构造函数
2) 通过set方法给属性注入值
3) p名称空间
4)自动装配(了解)
5) 注解
user类
package com.huan.Bean;
/**
* Created by 马欢欢 on 2017/6/24.
*/
public class User {
private int id;
private String username;
private String password;
public User() {
super();
System.out.println("创建user无参数构造器");
}
public User(int id, String username) {
System.out.println("创建user带参数的构造器");
this.id = id;
this.username = username;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
1.通过构造函数给对象的属性赋值
配置文件()
<bean id="user" class="com.huan.Bean.User">
<constructor-arg value="600" name="id"></constructor-arg>
<constructor-arg value="小钱" name="username"></constructor-arg>
</bean>
创建user对象
@Test
public void test(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("Spring/applicationContext.xml");
User user = (User) applicationContext.getBean("user");
System.out.println(user.getId()+""+user.getUsername());
}
2.通过set方法给属性注入值
配置文件()
<bean id="user" class="com.huan.Bean.User">
<property name="id" value="700"></property>
<property name="username" value="小后"></property>
</bean>
创建user对象
@Test
public void test(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("Spring/applicationContext.xml");
User user = (User) applicationContext.getBean("user");
System.out.println(user.getId()+""+user.getUsername());
}
2.1(常用)Set方法注入值
userDao.java
package com.huan.BeanTest;
/**
* Created by 马欢欢 on 2017/6/24.
*/
public class UserDao {
public void save(){
System.out.println("db:操作数据库");
}
}
UserService.java
package com.huan.BeanTest;
/**
* Created by 马欢欢 on 2017/6/24.
*/
public class UserService {
private UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public void save(){
userDao.save();
}
}
UserAction.java
package com.huan.BeanTest;
/**
* Created by 马欢欢 on 2017/6/24.
*/
public class UserAction {
private UserService userService;
public void setUserService(UserService userService) {
this.userService = userService;
}
public void execute(){
userService.save();
}
}
配置文件(方法一:推荐)
<!--Dao-->
<bean id="userDao" class="com.huan.BeanTest.UserDao"></bean>
<!--Service-->
<bean id="userService" class="com.huan.BeanTest.UserService">
<property name="userDao" ref="userDao"></property>
</bean>
<!--Action-->
<bean id="userAction" class="com.huan.BeanTest.UserAction">
<property name="userService" ref="userService"></property>
</bean>
配置文件(方法二:不推荐)
<bean id="userAction" class="cn.itcast.c_property.UserAction">
<property name="userService">
<bean class="cn.itcast.c_property.UserService">
<property name="userDao">
<bean class="cn.itcast.c_property.UserDao"></bean>
</property>
</bean>
</property>
</bean>
创建user对象
@Test
public void test2(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("Spring/applicationContext.xml");
UserAction userAction = (UserAction) applicationContext.getBean("userAction");
userAction.execute();
}
3.p名称空间
配置文件
<bean id="userDao" class="com.huan.BeanTest.UserDao"></bean>
<bean id="userService" class="com.huan.BeanTest.UserService" p:userDao-ref="userDao"></bean>
<bean id="userAction" class="com.huan.BeanTest.UserAction" p:userService-ref="userService"></bean>
创建user对象
@Test
public void test2(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("Spring/applicationContext.xml");
UserAction userAction = (UserAction) applicationContext.getBean("userAction");
userAction.execute();
}
注:
传统的注入:
<bean id="user" class="cn.itcast.c_property.User" >
<property name="name" value="huan"></property>
</bean>
p名称空间优化后
<bean id="user" class="cn.itcast.c_property.User" p:name="huan"></bean>
.
4.通过自动装配
配置文件(方法一)自动去IOC容器中找与属性名同名的引用的对象,并自动注入
<?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"
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 id="userDao" class="com.huan.BeanTest.UserDao"></bean>
<bean id="userService" class="com.huan.BeanTest.UserService" autowire="byName" ></bean>
<bean id="userAction" class="com.huan.BeanTest.UserAction" autowire="byName" ></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"
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"
default-autowire="byName">
<!--自动装配-->
<bean id="userDao" class="com.huan.BeanTest.UserDao"></bean>
<bean id="userService" class="com.huan.BeanTest.UserService" ></bean>
<bean id="userAction" class="com.huan.BeanTest.UserAction" ></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"
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 id="userDao" class="com.huan.BeanTest.UserDao"></bean>
<bean id="userService" class="com.huan.BeanTest.UserService" autowire="byType" ></bean>
<bean id="userAction" class="com.huan.BeanTest.UserAction" autowire="byType"></bean>
创建user对象
@Test
public void test2(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("Spring/applicationContext.xml");
UserAction userAction = (UserAction) applicationContext.getBean("userAction");
userAction.execute();
}
这种状态报错 只能有一个类型的对象
<bean id="userDao" class="cn.itcast.d_auto.UserDao"></bean>
<bean id="userService" class="cn.itcast.d_auto.UserService"></bean>
<!-- 如果根据类型自动装配: 必须确保IOC容器中只有一个该类型的对象 -->
<bean id="userAction" class="cn.itcast.d_auto.UserAction"></bean>
<!-- 报错: 因为上面已经有一个该类型的对象,且使用了根据类型自动装配-->
<bean id="userService_test" class="cn.itcast.d_auto.UserService" autowire="byType"></bean>
注:
Spring提供的自动装配主要是为了简化配置; 但是不利于后期的维护。
(一般不推荐使用)
上一篇:Spring--IOC容器——对象创建