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)的更多相关文章

  1. Spring 自动装配 byName

    自动装配 byName,这种模式由属性名称(方法名)指定自动装配.Spring 容器看作 beans,在 XML 配置文件中 beans 的 auto-wire 属性设置为 byName.然后,它尝试 ...

  2. Spring按名称自动装配--byName

    在Spring中,“按名称自动装配”是指,如果一个bean的名称与其他bean属性的名称是一样的,那么将自动装配它. 例如,如果“customer” bean公开一个“address”属性,Sprin ...

  3. Spring -08 -自动注入 -byName/byType/constructor -全局使用default-autowire=” byName"

    1.在Spring 配置文件中对象名和ref=”id”id 名相同使用自动注入,可以不配置<property/>2.两种配置办法 2.1在<bean>中通过 autowire= ...

  4. Spring autowire自动装配 ByType和ByName

    不使用自动装配前使用的是类的引用: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns=& ...

  5. spring 自动装配 default-autowire=&quot;byName/byType&quot;

    <PRE class=html name="code">spring 自动装配 default-autowire="byName/byType"   ...

  6. [译]15-spring 自动装配

    前面的章节我们已经学习了如何使用bean元素在xml配置文件中声明一个bean.也学习了如何使用bean的子元素contructor-arg 和property进行bean的依赖项的注入. 之前bea ...

  7. Spring 学习笔记(五)—— Bean之间的关系、作用域、自动装配

    继承 Spring提供了配置信息的继承机制,可以通过为<bean>元素指定parent值重用已有的<bean>元素的配置信息. <?xml version="1 ...

  8. Spring学习笔记 5. 尚硅谷_佟刚_Spring_自动装配

    1,回顾以前的做法 一个人有姓名,有住址,有一辆车.其中住址和车也是一个类,这种情况下不用自动装配是十分容易实现的 (1)Person类 package com.zsq; public class P ...

  9. spring第一课,beans配置(中)——自动装配

    •Spring IOC 容器可以自动装配 Bean. 需要做的仅仅是在 <bean> 的 autowire 属性里指定自动装配的模式 •byType(根据类型自动装配): 若 IOC 容器 ...

随机推荐

  1. 学习笔记:GLSL Core Tutorial – Pipeline (OpenGL 3.2 – OpenGL 4.2)

    GLSL Core Tutorial – Pipeline (OpenGL 3.2 – OpenGL 4.2) GLSL 是一种管道,一种图形化的流水线 1.GLSL 的具体工作流程: 简化流程如下: ...

  2. this与super使用总结(java)

    this: Java关键字this只能用于方法方法体内.当一个对象创建后,Java虚拟机(JVM)就会给这个对象分配一个引用自身的指针,这个指针的名字就是this.因此,this只能在类中的非静态方法 ...

  3. 在iOS中获取UIView的所有层级结构 相关

    在iOS中获取UIView的所有层级结构 应用场景 在实际 iOS 开发中,很多时候都需要知道某个 UI 控件中包含哪些子控件,并且分清楚它们的层级结构和自个的 frame 以及 bounds ,以便 ...

  4. JS定时器的使用--数码时钟

    <title>无标题文档</title> <script> function toDou(n){ if(n<10){ return '0'+n; }else{ ...

  5. sql 指定范围 获取随机数

    DECLARE @nMinimumCount INT= 1DECLARE @nMaximumCount INT= 100SELECT abs(CHECKSUM(NEWID()))%(@nMaximum ...

  6. java 文件字符输入、输出流

    Example10_6.java import java.io.*; public class Example10_6 { public static void main(String args[]) ...

  7. PAT (Advanced Level) 1114. Family Property (25)

    简单DFS. #include<cstdio> #include<cstring> #include<cmath> #include<vector> # ...

  8. java工程开发之图形化界面之(第五课)

    下面我们将: 一)更加完整的解释Graphics类 二)使用方法来更清晰的重新编写前面图形小应用程序之一 三)介绍一些其他的绘图的方法 四)介绍方法init,它是类似于paint但是用于不同用途的另一 ...

  9. sphinx multi valued filter

    publn_date is multi-valued <?php ini_set('memory_limit', '-1'); ini_set('max_execution_time', '10 ...

  10. java+tomcat+Eclipse+mysql配置

    Java下载及配置: 1. 下载:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html ...