Spring系列之基于获取容器的bean

基于XML配置获取容器中bean

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="
                    http://www.springframework.org/schema/context
                    http://www.springframework.org/schema/context/spring-context.xsd
                    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.xsd">
       <!--配置注解扫描-->
       <context:component-scan base-package="com.ithm"></context:component-scan>
       <!--   开启注解类型的AOP配置-->
       <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>

放入容器的bean 

package com.ithm.service;

/***
 * 被代理的对象
 */

import org.springframework.stereotype.Component;
@Component("iProxyProduceService")
public class ProxyProduceServiceImpl implements IProxyProduceService {

    public void saleProduce(Float money) {
        System.out.println("销售产品价格是====="+money);
    }

    public void serviceProduce() {
        System.out.println("产品售后");
    }
}

基于xml获取bean 

    @Test
     public  void  testAop(){
        ApplicationContext context= new ClassPathXmlApplicationContext("applicationContext.xml");
        IProxyProduceService service1 =(IProxyProduceService)context.getBean("iProxyProduceService");
        float money = 20000f;
        service1.saleProduce(money);
        service1.serviceProduce();
    }

基于注解获取获取IOC容器bean

基于注解的IOC容器配置

package com.ithm.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

@Configuration
//扫描包
@ComponentScan("com.ithm")
//开启AOP代理
@EnableAspectJAutoProxy
public class SpringConfigration {






}
    @Test
    public  void annotionConfiguration(){
        ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfigration.class);
        IProxyProduceService service =(IProxyProduceService)context.getBean("iProxyProduceService");
        float money = 20000f;
        service.saleProduce(money);
        service.serviceProduce();
    }

 

每个Test注解都初始化一边容器麻烦,优化一

public class TestDemo {

    private  ApplicationContext context;
    private IProxyProduceService service;

    @Before
    public void init(){
        context = new AnnotationConfigApplicationContext(SpringConfigration.class);
    }

    @Test
    public  void annotionConfiguration(){
        service =(IProxyProduceService)context.getBean("iProxyProduceService");
        float money = 20000f;
        service.saleProduce(money);
        service.serviceProduce();
    }
}

 由于Junit测试类版本原因报错:java.lang.NoClassDefFoundError: javax/servlet/ServletContext

D:\Java\jdk1.8.0_181\bin\java.exe -ea -Didea.test.cyclic.buffer.size=1048576 "-javaagent:E:\idea\IntelliJ IDEA Community Edition 2019.2.1\lib\idea_rt.jar=58972:E:\idea\IntelliJ IDEA Community Edition 2019.2.1\bin" -Dfile.encoding=UTF-8 -classpath "E:\idea\IntelliJ IDEA Community Edition 2019.2.1\lib\idea_rt.jar;E:\idea\IntelliJ IDEA Community Edition 2019.2.1\plugins\junit\lib\junit-rt.jar;E:\idea\IntelliJ IDEA Community Edition 2019.2.1\plugins\junit\lib\junit5-rt.jar;D:\Java\jdk1.8.0_181\jre\lib\charsets.jar;D:\Java\jdk1.8.0_181\jre\lib\deploy.jar;D:\Java\jdk1.8.0_181\jre\lib\ext\access-bridge-64.jar;D:\Java\jdk1.8.0_181\jre\lib\ext\cldrdata.jar;D:\Java\jdk1.8.0_181\jre\lib\ext\dnsns.jar;D:\Java\jdk1.8.0_181\jre\lib\ext\jaccess.jar;D:\Java\jdk1.8.0_181\jre\lib\ext\jfxrt.jar;D:\Java\jdk1.8.0_181\jre\lib\ext\localedata.jar;D:\Java\jdk1.8.0_181\jre\lib\ext\nashorn.jar;D:\Java\jdk1.8.0_181\jre\lib\ext\sunec.jar;D:\Java\jdk1.8.0_181\jre\lib\ext\sunjce_provider.jar;D:\Java\jdk1.8.0_181\jre\lib\ext\sunmscapi.jar;D:\Java\jdk1.8.0_181\jre\lib\ext\sunpkcs11.jar;D:\Java\jdk1.8.0_181\jre\lib\ext\zipfs.jar;D:\Java\jdk1.8.0_181\jre\lib\javaws.jar;D:\Java\jdk1.8.0_181\jre\lib\jce.jar;D:\Java\jdk1.8.0_181\jre\lib\jfr.jar;D:\Java\jdk1.8.0_181\jre\lib\jfxswt.jar;D:\Java\jdk1.8.0_181\jre\lib\jsse.jar;D:\Java\jdk1.8.0_181\jre\lib\management-agent.jar;D:\Java\jdk1.8.0_181\jre\lib\plugin.jar;D:\Java\jdk1.8.0_181\jre\lib\resources.jar;D:\Java\jdk1.8.0_181\jre\lib\rt.jar;E:\proxydemo\target\test-classes;E:\proxydemo\target\classes;D:\maven-repository\org\springframework\spring-context\5.0.2.RELEASE\spring-context-5.0.2.RELEASE.jar;D:\maven-repository\org\springframework\spring-aop\5.0.2.RELEASE\spring-aop-5.0.2.RELEASE.jar;D:\maven-repository\org\springframework\spring-beans\5.0.2.RELEASE\spring-beans-5.0.2.RELEASE.jar;D:\maven-repository\org\springframework\spring-core\5.0.2.RELEASE\spring-core-5.0.2.RELEASE.jar;D:\maven-repository\org\springframework\spring-jcl\5.0.2.RELEASE\spring-jcl-5.0.2.RELEASE.jar;D:\maven-repository\org\springframework\spring-expression\5.0.2.RELEASE\spring-expression-5.0.2.RELEASE.jar;D:\maven-repository\org\aspectj\aspectjweaver\1.8.7\aspectjweaver-1.8.7.jar;D:\maven-repository\junit\junit\4.12\junit-4.12.jar;D:\maven-repository\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar;D:\maven-repository\org\springframework\spring-test\5.0.5.RELEASE\spring-test-5.0.5.RELEASE.jar;D:\maven-repository\cglib\cglib\2.1_3\cglib-2.1_3.jar;D:\maven-repository\asm\asm\1.5.3\asm-1.5.3.jar" com.intellij.rt.execution.junit.JUnitStarter -ideVersion5 -junit4 TestDemo,annotionConfiguration
四月 05, 2021 9:13:34 上午 org.springframework.test.context.support.AbstractTestContextBootstrapper getDefaultTestExecutionListenerClassNames
信息: Loaded default TestExecutionListener class names from location [META-INF/spring.factories]: [org.springframework.test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener, org.springframework.test.context.support.DependencyInjectionTestExecutionListener, org.springframework.test.context.support.DirtiesContextTestExecutionListener, org.springframework.test.context.transaction.TransactionalTestExecutionListener, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener]




java.lang.NoClassDefFoundError: javax/servlet/ServletContext

	at java.lang.Class.getDeclaredConstructors0(Native Method)
	at java.lang.Class.privateGetDeclaredConstructors(Class.java:2671)
	at java.lang.Class.getConstructor0(Class.java:3075)
	at java.lang.Class.getDeclaredConstructor(Class.java:2178)

 spring-test版本从5.0.5改称5.0.2

     <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <!--<scope>test</scope>-->
        </dependency>
        <!--导入spring集成Junit的坐标-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>

再次利用JUNIT改进 

import com.ithm.config.SpringConfigration;
import com.ithm.service.IProxyProduceService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes=SpringConfigration.class)
public class TestDemo {
    @Autowired
    private IProxyProduceService service=null;

    @Test
    public  void annotionConfiguration(){
        float money = 20000f;
        service.saleProduce(money);
        service.serviceProduce();
    }
}

测试一下OK

 

 

总结:

1.两种方式初始化容器使用junit测试注意:

junit加载配置文件XML方式:

@ContextConfiguration(locations = "classpath:applicationContext.xml")

junit加载配置类java文件方式:

@ContextConfiguration(classes=SpringConfigration.class)

 

2.使用代码初始化容器

基于xml加载容器

  ApplicationContext context= new ClassPathXmlApplicationContext("applicationContext.xml");

 基于配置类加载容器

   ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfigration.class);

3.spring集成Junit测试版本问题 

查找了MAVEN仓库半天没有找到Junit和Spring-test或者Spring版本的对应关系(有知道的可以告诉我,我是尝试的降低版本问题才解决的)

这个手动创建的引入坐标,容易出现版本不兼容问题,建议还是创建项目的时候,通过选择比较好。

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值