spring注入bean两种方式(属性注入,构造器注入)
2015-06-02 10:03
731 查看
利用Spring的IOC实现简单小程序,Spring推荐接口编程,这里定义两个接口:IDao,IService,以及它们的实现类IDaoImpl,IServiceImpl,代码如下:
package DAO;
public interface IDao {
public String sayHello(String name,String sex);
}
package DAO;
public class IDaoImpl implements IDao {
private String c_name;
private int age;
public IDaoImpl(String c_name, int age) {
super();
this.c_name = c_name;
this.age = age;
System.out.println("我叫 " + c_name + "今年"+age+"岁");
}
public String sayHello(String name, String sex) {
return "Hello " + name + sex;
}
}
package service;
public interface IService {
public void service( String name, String sex);
}
package service;
import DAO.IDao;
public class IServiceImpl implements IService {
IDao dao;
public IDao getDao() {
return dao;
}
public void service(String name, String sex) {
System.out.println(dao.sayHello(name,sex));
}
public void setDao(IDao dao) {
this.dao = dao;
}
}
配置文件applicationContext.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" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"> <bean id="daoImpl" class="DAO.IDaoImpl">
<constructor-arg index="1" name="age" value="22"></constructor-arg>
<constructor-arg index="0" name="name" value="Jim"></constructor-arg>
</bean>
<bean id="service" class="service.IServiceImpl">
<property name="dao" ref="daoImpl"></property>
</bean>
</beans>
写一个测试代码的类Test:
package action;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import service.IService;
public class Test {
public static void main(String[] args) {
BeanFactory fac = new XmlBeanFactory(new ClassPathResource("applicationContext.xml"));
IService is = (IService) fac.getBean("service");
is.service("Lucy","女士");
}
}
运行代码结果:
我叫 Jim今年22岁
Hello Lucy女士
此例子中用到了spring注入bean的常用两种方式:依赖属性注入(setter方法<property>...</property>),构造器方式注入(<constructor-arg >...<constructor-arg />)
package DAO;
public interface IDao {
public String sayHello(String name,String sex);
}
package DAO;
public class IDaoImpl implements IDao {
private String c_name;
private int age;
public IDaoImpl(String c_name, int age) {
super();
this.c_name = c_name;
this.age = age;
System.out.println("我叫 " + c_name + "今年"+age+"岁");
}
public String sayHello(String name, String sex) {
return "Hello " + name + sex;
}
}
package service;
public interface IService {
public void service( String name, String sex);
}
package service;
import DAO.IDao;
public class IServiceImpl implements IService {
IDao dao;
public IDao getDao() {
return dao;
}
public void service(String name, String sex) {
System.out.println(dao.sayHello(name,sex));
}
public void setDao(IDao dao) {
this.dao = dao;
}
}
配置文件applicationContext.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" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"> <bean id="daoImpl" class="DAO.IDaoImpl">
<constructor-arg index="1" name="age" value="22"></constructor-arg>
<constructor-arg index="0" name="name" value="Jim"></constructor-arg>
</bean>
<bean id="service" class="service.IServiceImpl">
<property name="dao" ref="daoImpl"></property>
</bean>
</beans>
写一个测试代码的类Test:
package action;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import service.IService;
public class Test {
public static void main(String[] args) {
BeanFactory fac = new XmlBeanFactory(new ClassPathResource("applicationContext.xml"));
IService is = (IService) fac.getBean("service");
is.service("Lucy","女士");
}
}
运行代码结果:
我叫 Jim今年22岁
Hello Lucy女士
此例子中用到了spring注入bean的常用两种方式:依赖属性注入(setter方法<property>...</property>),构造器方式注入(<constructor-arg >...<constructor-arg />)
相关文章推荐
- 疯狂Java学习笔记(68)-----------synchronized
- Java并发编程-CAS
- MyEclipse Hibernate Reverse Engineering 找不到项目错误
- 使用Java API创建(create),查看(describe),列举(list),删除(delete)Kafka主题(Topic)--转载
- eclipse项目文件上的红色感叹号
- Myeclipse环境Spring版本升级后Web Server启动失败
- Java普通工程转换成标准的maven工程
- JDK8 - The type java.util.Map$Entry cannot be resolved
- spring定时器时batch批量操作出现找不到session的解决办法
- 学习:二维码、QR码、J4L-QRCode、java
- Spring.Net 配置文件
- Spring Roo 实站( 一 )部署安装 & 第一个示例程序
- java实现在线用户统计
- myeclipse自定义java注释
- Eclipse-----快速打开当前工程目录 浏览文件插件 EasyExplorer 和 OpenExplorer
- Spring Roo 实站( 一 )部署安装 & 第一个示例程序
- 疯狂Java学习笔记(67)-----------Timer和TimerTask
- 【struts2】五大核心jar包 和 作用
- 集算器用作Java结构化文件计算类库
- 【Android】eclipse下gradle