Sping--自动装配(byname, bytype)
UserDAOImpl.java:
package com.bjsxt.dao.impl; import com.bjsxt.dao.UserDAO;
import com.bjsxt.model.User; public class UserDAOImpl implements UserDAO { private int daoId; public int getDaoId() {
return daoId;
} public void setDaoId(int daoId) {
this.daoId = daoId;
} public void save(User user) {
//Hibernate
//JDBC
//XML
//NetWork
System.out.println("user saved!");
} @Override
public String toString() {
return "daoId=" + daoId;
} }
UserService.java:
package com.bjsxt.service;
import com.bjsxt.dao.UserDAO;
import com.bjsxt.model.User; public class UserService {
private UserDAO userDAO;
public void add(User user) {
userDAO.save(user);
}
public UserDAO getUserDAO() {
return userDAO;
}
public void setUserDAO(UserDAO userDAO) {
this.userDAO = userDAO;
}
}
UserServiceTest.java:
package com.bjsxt.service;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.bjsxt.model.User; //Dependency Injection
//Inverse of Control
public class UserServiceTest {
@Test
public void testAdd() throws Exception {
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
UserService service = (UserService)ctx.getBean("userService");
System.out.println(service.getUserDAO());
}
}
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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"
> <bean name="userDAO" class="com.bjsxt.dao.impl.UserDAOImpl">
<property name="daoId" value="1"></property>
</bean> <bean name="userDAO2" class="com.bjsxt.dao.impl.UserDAOImpl">
<property name="daoId" value="2"></property>
</bean> <bean id="userService" class="com.bjsxt.service.UserService" scope="prototype" autowire="byName">
</bean> </beans>
结果: daoId=1
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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"
>
<bean name="userDAO2" class="com.bjsxt.dao.impl.UserDAOImpl">
<property name="daoId" value="2"></property>
</bean>
<bean id="userService" class="com.bjsxt.service.UserService" scope="prototype" autowire="byType">
</bean>
</beans>
结果: daoId=2
Sping--自动装配(byname, bytype)的更多相关文章
- Spring 自动装配 byName
自动装配 byName,这种模式由属性名称(方法名)指定自动装配.Spring 容器看作 beans,在 XML 配置文件中 beans 的 auto-wire 属性设置为 byName.然后,它尝试 ...
- Spring按名称自动装配--byName
在Spring中,“按名称自动装配”是指,如果一个bean的名称与其他bean属性的名称是一样的,那么将自动装配它. 例如,如果“customer” bean公开一个“address”属性,Sprin ...
- Spring -08 -自动注入 -byName/byType/constructor -全局使用default-autowire=” byName"
1.在Spring 配置文件中对象名和ref=”id”id 名相同使用自动注入,可以不配置<property/>2.两种配置办法 2.1在<bean>中通过 autowire= ...
- Spring autowire自动装配 ByType和ByName
不使用自动装配前使用的是类的引用: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns=& ...
- spring 自动装配 default-autowire="byName/byType"
<PRE class=html name="code">spring 自动装配 default-autowire="byName/byType" ...
- [译]15-spring 自动装配
前面的章节我们已经学习了如何使用bean元素在xml配置文件中声明一个bean.也学习了如何使用bean的子元素contructor-arg 和property进行bean的依赖项的注入. 之前bea ...
- Spring 学习笔记(五)—— Bean之间的关系、作用域、自动装配
继承 Spring提供了配置信息的继承机制,可以通过为<bean>元素指定parent值重用已有的<bean>元素的配置信息. <?xml version="1 ...
- Spring学习笔记 5. 尚硅谷_佟刚_Spring_自动装配
1,回顾以前的做法 一个人有姓名,有住址,有一辆车.其中住址和车也是一个类,这种情况下不用自动装配是十分容易实现的 (1)Person类 package com.zsq; public class P ...
- spring第一课,beans配置(中)——自动装配
•Spring IOC 容器可以自动装配 Bean. 需要做的仅仅是在 <bean> 的 autowire 属性里指定自动装配的模式 •byType(根据类型自动装配): 若 IOC 容器 ...
随机推荐
- Struts2 请求数据的自动封装 及 自定义转换器类
请求数据自动封装: 实现原理:使用了参数拦截器.struts-default.xml中 <interceptor name="params" class="com. ...
- linux视频学习3(linux安装,shell,tcp/ip协议,网络配置)
linux系统的安装: 1.linux系统的安装方式三种: 1.独立安装linux系统. 2.虚拟机安装linux系统. a.安装虚拟机,基本是一路点下去. b.安装linux. c.linux 安装 ...
- Notes over compiling..
When compiling VIM on windows, using nmake may be a better choice.. Because so far my attempts to co ...
- Swift 使用Extension 场景 浅析
别人一看到我的 Swift 代码,立刻就会问我为什么如此频繁的使用 extension.这是前几天在我写的另一篇文章中收到的评论: 我大量使用 extension 的主要目的是为了提高代码可读性.以下 ...
- ALAssetsLibrary 照片相关 浅析
ALAssetsLibrary 提供了访问iOS设备下”照片”应用下所有照片和视频的接口: 从 ALAssetsLibrary 中可读取所有的相册数据,即 ALAssetsGroup 对象列表: 从每 ...
- 网络获取数据的Xml的Pull解析
网络获取的XML的Pull解析 <?xml version="1.0" encoding="utf-8" ?> - <students> ...
- java类对象
不错的文章 原文地址:(转载)java中的Class类与Class对象作者:albert1017 本文用作笔记之用,引用的网上资料: http://www.blogjava.net/formatmys ...
- Windows编译安装mod_wsgi,配合使用Django+Apahce
编译环境: 均是32位版本 Microsoft Visual Studio 10.0 Microsoft SDKs v7.1 Apache2.4 Python3.4 mod_wsgi-4.5.0 要求 ...
- 转: JMeter技巧集锦
from:http://java.chinaitlab.com/tools/355421.html JMeter 是一个流行的用于负载测试的开源工具, 具有许多有用的功能元件,如线程组(thread ...
- hosts文件不显示
1.按住windows键(就是小旗子小窗口键)+R,打开运行:2.在输入框中输入 cmd并回车:3.紧接着在dos窗口中输入"CD \WINDOWS\SYSTEM32\DRIVERS\ETC ...