|
/spring_0004_bean的注入方法/src/com/b510/dao/AnimaleDao.java
1 package com.b510.dao;
2 /**
3 * 动物抽象类
4 * @author Hongten
5 *
6 */
7 public interface AnimaleDao {
8
9 /**
10 * 动物的say()方法
11 */
12 public abstract void say();
13
14 }
复制代码
/spring_0004_bean的注入方法/src/com/b510/dao/impl/DogDaoImpl.java
1 package com.b510.dao.impl;
2
3 import com.b510.dao.AnimaleDao;
4
5
6 public class DogDaoImpl implements AnimaleDao {
7
8 private String name;
9
10 public String getName() {
11 return name;
12 }
13
14 public void setName(String name) {
15 this.name = name;
16 }
17
18 /* (non-Javadoc)
19 * @see com.b510.dao.impl.AnimaleDao#say()
20 */
21 public void say(){
22 System.out.println(name+" is a dog,he likes bone");
23 }
24 }
复制代码
/spring_0004_bean的注入方法/src/com/b510/service/ServiceBean.java
1 package com.b510.service;
2
3 /**
4 * ServiceBean接口
5 *
6 * @author Hongten
7 *
8 */
9 public interface ServiceBean {
10
11 /**
12 * 输出相关信息
13 */
14 public abstract void myPetSay();
15
16 }
复制代码
/spring_0004_bean的注入方法/src/com/b510/service/impl/ServiceBeanImpl.java
1 package com.b510.service.impl;
2
3 import com.b510.dao.AnimaleDao;
4 import com.b510.service.ServiceBean;
5 /**
6 * ServiceBean的一个实现类
7 *
8 * @author Hongten
9 *
10 */
11 public class ServiceBeanImpl implements ServiceBean {
12
13 private AnimaleDao animaleDao;
14
15 /* (non-Javadoc)
16 * @see com.b510.service.impl.ServiceBean#myPetSay()
17 */
18 public void myPetSay(){
19 animaleDao.say();
20 }
21
22 public AnimaleDao getAnimaleDao() {
23 return animaleDao;
24 }
25
26 /**
27 * 设置一个AnimaleDao构造函数,Spring在注入的时候,就是调用此方法进行构造注入的
28 * 所以在使用构造注入的时候,一定要有此构造函数
29 * @param animaleDao AnimaleDao的一个实例
30 */
31 public void setAnimaleDao(AnimaleDao animaleDao) {
32 this.animaleDao = animaleDao;
33 }
34 }
复制代码
/spring_0004_bean的注入方法/src/beans.xml
1 <?xml version="1.0" encoding="UTF-8"?>
2 <beans xmlns="http://www.springframework.org/schema/beans"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xsi:schemaLocation="http://www.springframework.org/schema/beans
5 http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
6 <bean id="animaleDao" class="com.b510.dao.impl.DogDaoImpl">
7 <property name="name" value="哈巴"></property>
8 </bean>
9 <bean id="serviceDao" class="com.b510.service.impl.ServiceBeanImpl">
10 <property name="animaleDao" ref="animaleDao"></property>
11 </bean>
12 </beans>
复制代码
/spring_0004_bean的注入方法/src/com/b510/test/SpringTest.java
1 package com.b510.test;
2
3 import org.springframework.context.ApplicationContext;
4 import org.springframework.context.support.ClassPathXmlApplicationContext;
5
6 import com.b510.service.ServiceBean;
7
8 public class SpringTest {
9 public static void main(String[] args) {
10 ApplicationContext ctx=new ClassPathXmlApplicationContext("beans.xml");
11 ServiceBean serviceBean=(ServiceBean) ctx.getBean("serviceDao");
12 serviceBean.myPetSay();
13 }
14 }
复制代码
运行效果:
1 2012-3-7 23:34:52 org.springframework.context.support.AbstractApplicationContext prepareRefresh
2 信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@15eb0a9: display name [org.springframework.context.support.ClassPathXmlApplicationContext@15eb0a9]; startup date [Wed Mar 07 23:34:52 CST 2012]; root of context hierarchy
3 2012-3-7 23:34:52 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
4 信息: Loading XML bean definitions from class path resource [beans.xml]
5 2012-3-7 23:34:52 org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory
6 信息: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@15eb0a9]: org.springframework.beans.factory.support.DefaultListableBeanFactory@161f10f
7 2012-3-7 23:34:52 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
8 信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@161f10f: defining beans [animaleDao,serviceDao]; root of factory hierarchy
9 哈巴 is a dog,he likes bone
复制代码 |
|