Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 821|回复: 0

spring开发_spring构造注入Bean

[复制链接]

该用户从未签到

发表于 2012-3-8 15:17:43 | 显示全部楼层 |阅读模式
/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
复制代码
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

GMT+8, 2024-4-30 06:16 , Processed in 0.383900 second(s), 46 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表