标签:
spring中如何给对象的属性复制?
1)通过构造方法
2)通过set方法给属性注入值
3)p命名空间
4)自动转配(了解即可,不推荐使用)
5)注解
准备工作(模拟业务方法)Action-->service-->dao
1)UserDao:
p<span style="font-family:Courier New;">ublic class UserDao { public void save(){ System.out.println("save方法"); } }</span>2)UserService:
p<span style="font-family:Courier New;">ublic class UserService { private UserDao userDao; public void setUserDao(UserDao userDao) { this.userDao = userDao; } public void save(){ userDao.save(); } }</span>3)UserAction:
<span style="font-family:Courier New;">public class UserAction { private UserService userService; public void setUserService(UserService userService) { this.userService = userService; } public String execute(){ userService.save(); return null; } }</span>4)App测试类:
public class App { ApplicationContext ac=new ClassPathXmlApplicationContext("cn/itcast/property/bean.xml"); @Test public void test(){ UserAction userAction=(UserAction) ac.getBean("userAction"); System.out.println(userAction.execute()); } }5)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: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"> </beans>方式一.set方法注入
1)必须保证有set方法
<bean id="userDao" class="cn.itcast.property.UserDao"></bean> <bean id="userService" class="cn.itcast.property.UserService"> <property name="userdao" ref="userDao"></property> </bean> <bean id="userAction" class="cn.itcast.property.UserAction"> <property name="userService" ref="userService"></property> </bean>方式二.内部bean(因为是内部bean 因此如果还有一个action 就不能引用里面的service方法)
bean id="userAction" class="cn.itcast.property.UserAction"> <property name="userService"> <bean id="userService" class="cn.itcast.property.UserService"> <property name="userDao"> <bean id="userDao" class="cn.itcast.property.UserDao"></bean> </property> </bean> </property> </bean>方式三.p命名空间
<bean id="userDao" class="cn.itcast.property.UserDao"></bean> <bean id="userService" class="cn.itcast.property.UserService" p:userdao-ref="userDao"></bean> <bean id="userAction" class="cn.itcast.property.UserAction" p:userService-ref="userService"></bean>方式四:自动装配(不推荐使用)
<!-- 根据“名称”自动装配: userAction注入的属性,会去ioc容器中自动查找与属性同名的对象 -->
<bean id="userDao" class="cn.itcast.property.UserDao" ></bean> <bean id="userService" class="cn.itcast.property.UserService" autowire="byName"></bean> <bean id="userAction" class="cn.itcast.property.UserAction" autowire="byName"></bean>方式五:注解
使用注解步骤:
1)先引入context名称空间
xmlns:context="http://www.springframework.org/schema/context"
2)开启注解扫描
<context:component-scanbase-package="cn.itcast.e_anno2"></context:component-scan>
3)使用注解
通过注解的方式,把对象加入ioc容器。
创建对象以及处理对象依赖关系,相关的注解:
@Component 指定把一个对象加入IOC容器
@Repository 作用同@Component; 在持久层使用
@Service 作用同@Component; 在业务逻辑层使用
@Controller 作用同@Component; 在控制层使用
@Resource 属性注入
<!-- 开启扫描机制 --> <context:component-scan base-package="cn.itcast.property"></context:component-scan>UserDao使用注解(使用注解就不用set方法了)
@Repository public class UserDao { public void save(){ System.out.println("save方法"); } }UserService使用注解:
@Service public class UserService { @Resource private UserDao userDao; public void save(){ userDao.save(); } }UserAction使用注解
@Controller public class UserAction { @Resource private UserService userService; public String execute(){ userService.save(); return null; } }
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:
原文地址:http://blog.csdn.net/u014010769/article/details/47134997