spring开发_spring构造注入Bean
项目结构:
http://www.cnblogs.com/hongten/gallery/image/112399.html
/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
标签:
spring 构造注入 java
【推荐】博客园的心动:当一群程序员决定开源共建一个真诚相亲平台
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】Flutter适配HarmonyOS 5知识地图,实战解析+高频避坑指南
【推荐】凌霞软件回馈社区,携手博客园推出1Panel与Halo联合终身会员
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· C# 代码如何影响 CPU 缓存速度?
· 智能桌面机器人:使用 .NET 为树莓派开发 Wifi 配网功能
· C# 模式匹配全解:原理、用法与易错点
· 记一次SSD性能瓶颈排查之路——寿命与性能之间的取舍
· 理解 .NET 结构体字段的内存布局
· 【故障公告】6月9日 17:24~17:34 再次遭遇攻击(晚上遭遇更疯狂的攻击)
· 3 个超火的开源项目「GitHub 热点速览」
· C#-Visual Studio工具使用实践
· 时隔半年,拾笔分享:来自一个大龄程序员的迷茫自问
· [原创]《C#高级GDI+实战:从零开发一个流程图》第02章:画一个矩形,能拖动!