您的位置:首页 > 编程语言 > Java开发

Spring 注入

2015-07-21 10:38 459 查看
新建dao层接口以及service接口,分别模拟数据层,以及逻辑层代码,其中分别写入一个run()方法:

InjectionInterface.java

package dao.interfaces;

public interface InjectionInterface {

public void run();

}


ServiceInterface.java

package service.interfaces;

public interface ServiceInterface {
public void run();
}


新建一个dao层实现类,实现dao层接口方法:

InjectionImpl.java

package dao.interfacesimpl;

import dao.interfaces.InjectionInterface;

public class InjectionImpl implements InjectionInterface {

@Override
public void run() {

System.out.println("Spring Start!");

}

}


建立一个service层实现类,在其中定义一个私有对象,并可以分别以setter方法,和构造器进行传入,这就是两种注入方法:

ServiceImpl.java

package service.intefacesimpl;

import service.interfaces.ServiceInterface;
import dao.interfaces.InjectionInterface;

public class ServiceImpl implements ServiceInterface {

private InjectionInterface inject;

public ServiceImpl(InjectionInterface inject) {
super();
this.inject = inject;
}

public void setInject(InjectionInterface inject) {
this.inject = inject;
}

@Override
public void run() {
System.out.println("Service 业务逻辑!");
inject.run();
}

}


新建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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd" >

<!--         <bean id="injectionService" class="com.imooc.ioc.injection.service.InjectionServiceImpl"> -->
<!--            <property name="injectionDAO" ref="injectionDAO"></property> -->
<!--         </bean> -->

<bean id="serviceInterface" class="service.intefacesimpl.ServiceImpl">
<!-- ><property name="inject" ref="InjectionInterface"></property> -->
<constructor-arg name="inject" ref="InjectionInterface"></constructor-arg>
<!-- 此处name一定要和 service.intefacesimpl.ServiceImpl 中的InjectionImpl 参数名一致-->
</bean>

<bean id="InjectionInterface" class="dao.interfacesimpl.InjectionImpl"></bean>
</beans>


Bean配置项主要有如下几个:

id:代表唯一标识符

class:具体要实例化的哪一个类

scope:指具体范围,即作用域

Constructor arguments:构造器参数

Properties:设置注入.以上两个方法注入是,name一定要和Service层

的实现类里面参数一致,如上放代码里面的inject.

Autowriting mode:自动装配模式

lazy-initialization mode:懒加载模式

Initialization/destruction method:初始化和销毁的方法。

加载方式:

package test;

import org.junit.After;
import org.junit.Before;
import org.springframework.beans.BeansException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.util.StringUtils;

public class UnitTestBase {

private ClassPathXmlApplicationContext context;

private String springXmlPath;

public UnitTestBase(){}

public UnitTestBase(String springXml){
this.springXmlPath = springXml;
}

@Before
public void before(){
if(StringUtils.isEmpty(springXmlPath)){
springXmlPath = "classpath*:spring-*.xml";
}
try{
context = new ClassPathXmlApplicationContext(springXmlPath.split("[,\\s]+"));
context.start();
}catch(BeansException e){
e.printStackTrace();
}
}

@After
4000

public void after(){
context.destroy();
}

@SuppressWarnings(value = { "unchecked" })
protected <T extends Object>T getBean(String beanId){
return (T)context.getBean(beanId);
}

protected <T extends Object>T getBean(Class<T> clazz){
return (T)context.getBean(clazz);
}
}


通过ClassPathXmlApplicationContext ,实例化一个对象context,然后网其中传入xml文件的路径,调用start()方法,即可启动加载。

然后可以通过context.getBean()方法,可以通过传入的参数生成bean的实例,传入的参数可以是id,也可以是类的类型。

Tips:xml路径填写方式有多种,利用classpath的时候,一般未设置情况下从根目录开始,因此若是将配置文件放在包下,则会报错,书写格式如下:

classpath:com/lds/config/spring-injection.xml


包名之间不是用点,而是用 /

下面是测试代码:

package test;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.BlockJUnit4ClassRunner;

import service.interfaces.ServiceInterface;

@RunWith(BlockJUnit4ClassRunner.class)
public class TestInjection extends UnitTestBase{

public TestInjection(){
super("classpath:com/lds/config/spring-injection.xml");
}

@Test
public void testSetter(){
ServiceInterface interface1 = super.getBean("serviceInterface");
interface1.run();
}

@Test
public void testConstruct(){
ServiceInterface interface1 = super.getBean("serviceInterface");
interface1.run();
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息