spring依赖注入引发的一点思考

来源:互联网 发布:宇宙几维 知乎 编辑:程序博客网 时间:2024/04/30 16:49

java中bean 分为普通的java bean和spring的bean两种。

1)通过spring的配置文件或者注解,在进程启动时由spring对其初始化、并最终最终放到spring容器中,这就是spring的bean,否则就是java  bean。

2)如果是spring的bean,那么在代码中直接getbean("beanname")使用即可;如果是普通的java bean 使用时直接 new,产生一个实例即可。


一、下面举一个spring bean进行依赖注入的最简单例子:

1、service接口和类:

1)demoService接口和实现类:

public interface IDemoService {public String sayHello(String a);}
实现类:

import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;@Servicepublic class DemoService implements IDemoService {@Autowiredprivate ITestService testService;@Overridepublic String sayHello(String a) {System.out.println(testService.test("test"));return "hello"+a;}}

说明:该service实现类中又使用注解(@AutoWired)依赖注入了另一个spring bean ITestService


2)TestService接口和实现类:

public interface ITestService {public String test(String test);}
实现类:

@Servicepublic class TestService implements ITestService {@Overridepublic String test(String test) {return "test:"+test;}}

2、pom.xml文件:

<!-- spring --><dependency><groupId>org.springframework</groupId><artifactId>spring-core</artifactId><version>${org.springframework.version}</version><type>jar</type><scope>compile</scope></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-beans</artifactId><version>${org.springframework.version}</version><type>jar</type><scope>compile</scope></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>${org.springframework.version}</version><type>jar</type><scope>compile</scope></dependency><dependency>  <groupId>org.springframework</groupId>  <artifactId>spring-context-support</artifactId>  <version>${org.springframework.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-orm</artifactId><version>${org.springframework.version}</version></dependency><dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId><version>1.8.2</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>${org.springframework.version}</version></dependency><dependency>    <groupId>org.quartz-scheduler</groupId>    <artifactId>quartz</artifactId>    <version>2.2.0</version></dependency>

3、application.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:aop="http://www.springframework.org/schema/aop"   xmlns:context="http://www.springframework.org/schema/context"   xmlns:mvc="http://www.springframework.org/schema/mvc"   xmlns:tx="http://www.springframework.org/schema/tx"   xmlns:jaxws="http://cxf.apache.org/jaxws"   xmlns:task="http://www.springframework.org/schema/task"   xsi:schemaLocation="http://www.springframework.org/schema/beans   http://www.springframework.org/schema/beans/spring-beans.xsd   http://www.springframework.org/schema/aop   http://www.springframework.org/schema/aop/spring-aop-3.2.xsd   http://www.springframework.org/schema/context   http://www.springframework.org/schema/context/spring-context-3.2.xsd   http://www.springframework.org/schema/mvc   http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd   http://www.springframework.org/schema/tx   http://www.springframework.org/schema/tx/spring-tx-3.2.xsd   http://www.springframework.org/schema/task       http://www.springframework.org/schema/task/spring-task-3.2.xsd">      <!-- 注解方式的aop<aop:aspectj-autoproxy  proxy-target-class="true"/>  --><task:annotation-driven/>        <context:component-scan base-package="cn.eud.nuc.archiveTest.service" />    <!-- 自动搜索Sping的注解类--><context:component-scan base-package="cn.eud.nuc" ><context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan></beans> 

4、测试程序:

public class App {private static ApplicationContext ac;public static void main(String[] args) {ac = new ClassPathXmlApplicationContext(new String[] { "classpath:application.xml"});IDemoService demoService = (IDemoService)ac.getBean("demoService");System.out.println(demoService.sayHello(" sfsd"));}}

5、说明:

1)在App主类中,只能通过ac.getBean()的方式获取demoService 的spring bean来使用;下面这种方式使用是错误的。

@Servicepublic class  App{@Autowiredprivate IDemoService demoService;private static ApplicationContext ac;public static void main(String[] args) {ac = new ClassPathXmlApplicationContext(new String[] { "classpath:application.xml"});}private void test() {demoService.sayHello("sss:");}}
2)除了使用上面注解的方式去注入,也可以使用配置文件的方式来实现:

去掉TestService实现类的注解,

public class TestService implements ITestService {@Overridepublic String test(String test) {return "test:"+test;}}
在application.xml中添加:

<bean class="cn.eud.nuc.archiveTest.service.TestService" />


3)在DemoService实现类中,使用@AutoWired注入ITestService,spring会在进程启动的时候找ITestService的实现类,将其初始化,然后再将其注入到DemoService中,这样,就可以在DemoService中直接使用了。

二、使用代理的方式完成spring bean的注入:

1、service类:

1)demoService接口和实现类:

public interface IDemoService {public String sayHello(String a);}
实现类:

import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;@Servicepublic class DemoService implements IDemoService {@Autowiredprivate ITestService testService;@Overridepublic String sayHello(String a) {System.out.println(testService.test("test"));return "hello"+a;}}

说明:该service实现类中又使用注解(@AutoWired)依赖注入了另一个spring bean ITestService

2)TestService接口和实现类:

public interface ITestService {public String test(String test);}

这个接口没有实现类,下面我们使用一个动态代理类来代理这个接口(底层代理类会实现该接口),并将这个动态代理类注入到spring容器中,并初始化,然后注入到DemoService中。

2、代理类:

import java.lang.reflect.InvocationHandler;import java.lang.reflect.Method;import java.lang.reflect.Proxy;import org.springframework.beans.factory.FactoryBean;import org.springframework.beans.factory.InitializingBean;import cn.eud.nuc.archiveTest.service.ITestService;@SuppressWarnings({ "rawtypes" })public class ServiceProxy implements FactoryBean,InitializingBean{private Object proxyClient;private Class<?> objectClass;@Overridepublic void afterPropertiesSet() throws Exception {final ClassLoader classLoader = Thread.currentThread().getContextClassLoader();objectClass = classLoader.loadClass("cn.eud.nuc.archiveTest.service.ITestService");proxyClient = Proxy.newProxyInstance(classLoader, new Class[] { objectClass }, new InvocationHandler() {@Overridepublic Object invoke(Object proxy, Method method, Object[] args)throws Throwable {try {Class<ITestService> testClass = (Class<ITestService>)classLoader.loadClass("cn.eud.nuc.archiveTest.service.TestService");ITestService newInstance = testClass.newInstance();System.out.println("proxy:"+newInstance.test("a"));return method.invoke(newInstance, args);} catch (ClassNotFoundException e) {throw e;}}});}@Overridepublic Object getObject() throws Exception {return proxyClient;}@Overridepublic Class<?> getObjectType() {return objectClass;}@Overridepublic boolean isSingleton() {return true;}}

1)说明:

这个类首先实现了InitializingBean接口,目的是spring在加载这个类(通过application.xml配置文件)的时候调用afterPropertiesSet() 方法来实现代理类的创建;其次,该类也实现了FactoryBean接口,目的是供spring注入的时候返回正确的bean类型。


2)我们再随便写一个接口的实现类(不添加到spring中)

package cn.eud.nuc.archiveTest.service;public class TestService implements ITestService {@Overridepublic String test(String test) {return "test:"+test;}}


3、application.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:aop="http://www.springframework.org/schema/aop"   xmlns:context="http://www.springframework.org/schema/context"   xmlns:mvc="http://www.springframework.org/schema/mvc"   xmlns:tx="http://www.springframework.org/schema/tx"   xmlns:jaxws="http://cxf.apache.org/jaxws"   xmlns:task="http://www.springframework.org/schema/task"   xsi:schemaLocation="http://www.springframework.org/schema/beans   http://www.springframework.org/schema/beans/spring-beans.xsd   http://www.springframework.org/schema/aop   http://www.springframework.org/schema/aop/spring-aop-3.2.xsd   http://www.springframework.org/schema/context   http://www.springframework.org/schema/context/spring-context-3.2.xsd   http://www.springframework.org/schema/mvc   http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd   http://www.springframework.org/schema/tx   http://www.springframework.org/schema/tx/spring-tx-3.2.xsd   http://www.springframework.org/schema/task       http://www.springframework.org/schema/task/spring-task-3.2.xsd">      <!-- 注解方式的aop<aop:aspectj-autoproxy  proxy-target-class="true"/>  --><task:annotation-driven/>        <context:component-scan base-package="cn.eud.nuc.archiveTest.service" />    <!-- 自动搜索Sping的注解类--><context:component-scan base-package="cn.eud.nuc" ><context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan><bean class="cn.eud.nuc.archiveTest.ServiceProxy" /></beans> 

4、测试类:同上

5、说明:

spring在依赖注入的时候,需要找到@autoWired接口的实现类去注入。上面两个实例中,

1)第一种:将一个接口的实现类注册到spring容器中,spring自动去进行依赖注入(也是spring推荐正规的方式),

2)第二种:使用了一个自定义代理类,在代理类找到接口然后动态代理这个接口,然后将代理类交给spring去管理,这样代理类就相当于实现了该接口的一个类,并注入到DemoService中去使用






原创粉丝点击