Spring中给对象属性赋值
1、通过set方法给属性注入值
2、p名称空间
3、自动装配
4、注解
编写MVCModel调用userAction
MVCModel
public class MVCModel { ApplicationContext ac = new ClassPathXmlApplicationContext("com/isoftstone/xml/applicationContext.xml"); @Test public void test(){ UserAction userAction = (UserAction)ac.getBean("userAction"); userAction.save(); } }
编写UserAction 调用userService
UserAction
public class UserAction { private UserService userService; public void save(){ userService.save(); } public void setUserService(UserService userService) { this.userService = userService; } }
编写UserService 调用userDao
UserService
public class UserService { private UserDao userDao; public void save(){ userDao.save(); } public void setUserDao(UserDao userDao) { this.userDao = userDao; } }
编写UserDao
UserDao
public class UserDao { public void save(){ System.out.println("dao执行保存用户方法"); } }
编写applicationContext
<?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"> <!-- 通过set方法给属性赋值 --> <!-- <bean id="userDao" class="com.isoftstone.dao.UserDao"></bean> <bean id="userService" class="com.isoftstone.service.UserService"> <property name="userDao" ref="userDao"></property> </bean> <bean id="userAction" class="com.isoftstone.action.UserAction"> <property name="userService" ref="userService"></property> </bean> --> <!--set方法给属性赋值的另一种形式 内部bean --> <!-- <bean id="userAction" class="com.isoftstone.action.UserAction"> <property name="userService"> <bean id="userService" class="com.isoftstone.service.UserService"> <property name="userDao"> <bean id="userDao" class="com.isoftstone.dao.UserDao"></bean> </property> </bean> </property> </bean> --> <!-- p名称空间给对象属性注入值 spring3.0以上版本支持--> <!-- <bean id="userDao" class="com.isoftstone.dao.UserDao"></bean> <bean id="userService" class="com.isoftstone.service.UserService" p:userDao-ref="userDao"></bean> <bean id="userAction" class="com.isoftstone.action.UserAction" p:userService-ref="userService"></bean> --> <!-- 自动装配 根据名称自动装配,自动去IOC容器中找与属性名同名的引用对象,并自动注入 还有一种写法是设置全局自动装配 default-autowire="byName"把这句话放在http://www.springframework.org/schema/context/spring-context.xsd"后面即可 如果是根据类型来装配必须确保容器中只有一个该类型的对象否则会报错--> <bean id="userDao" class="com.isoftstone.dao.UserDao" autowire="byName"></bean> <bean id="userService" class="com.isoftstone.service.UserService" autowire="byName"></bean> <bean id="userAction" class="com.isoftstone.action.UserAction" autowire="byName"></bean> </beans>
通过注解方式给对象属性赋值
applicationContext.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" 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"> <!-- 开启注解 --> <context:component-scan base-package="com.isoftstone"></context:component-scan> </beans>
DAO
import org.springframework.stereotype.Repository; @Repository public class UserDao { public void save(){ System.out.println("成功调用dao保存用户方法"); } }
Service
import javax.annotation.Resource; import org.springframework.stereotype.Service; import com.isoftstone.dao.UserDao; @Service public class UserService { @Resource private UserDao userDao; public void save(){ userDao.save(); } }
Action
import javax.annotation.Resource; import org.springframework.stereotype.Controller; import com.isoftstone.service.UserService; @Controller public class UserAction { @Resource private UserService userService; public void save(){ userService.save(); } }
调用
import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.isoftstone.action.UserAction; public class MVCModel { ApplicationContext ac = new ClassPathXmlApplicationContext("com/isoftstone/xml/applicationContext.xml"); @Test public void testIOC(){ UserAction userAction = (UserAction)ac.getBean("userAction"); userAction.save(); } }
如果出现
Caused by: java.lang.NoClassDefFoundError: org/springframework/aop/TargetSource
那是因为没有导入aop的jar包