前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >spring开发_spring构造注入Bean

spring开发_spring构造注入Bean

作者头像
Hongten
发布2018-09-13 16:54:31
4440
发布2018-09-13 16:54:31
举报
文章被收录于专栏:HongtenHongten

项目结构:

http://www.cnblogs.com/hongten/gallery/image/112399.html

/spring_0004_bean的注入方法/src/com/b510/dao/AnimaleDao.java

代码语言:javascript
复制
 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

代码语言:javascript
复制
 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

代码语言:javascript
复制
 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

代码语言:javascript
复制
 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

代码语言:javascript
复制
 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

代码语言:javascript
复制
 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 }

运行效果:

代码语言:javascript
复制
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
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2012-03-07 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档