spring AOP

AOP    面向切面编程    

下来先说 前置和后置增强  

1 前置增强  需要一个类 实现 

implements MethodBeforeAdvice 以下接口 ,并且实现接口中的方法 

public class lasraop implements MethodBeforeAdvice {
    public void before(Method method, Object[] objects, Object o) throws Throwable {
        System.out.println("bengin");
    }
}

2 后置增强  需要一个类 实现

implements AfterReturningAdvice
以下接口 ,并且实现接口中的方法  
public class fristaop implements AfterReturningAdvice {
    public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {

        System.out.println("日志记录");

    }
}

   使用方法  

 applicationContext 文件中 在头文件 中 插入 

xmlns:aop="http://www.springframework.org/schema/aop" 

接下来为具体使用
  <bean id="first" class="cn.study.aop.fristaop"></bean>

    <bean id="last" class="cn.study.aop.lasraop"></bean>

 

一个为前置增强 一个为后置增强 的 bean  

    <aop:config>
        <aop:pointcut id="myPoint" expression="execution(* * ..serviers.*.*(..))"></aop:pointcut>
        <aop:advisor advice-ref="last" pointcut-ref="myPoint"></aop:advisor>
        <aop:advisor advice-ref="first" pointcut-ref="myPoint"></aop:advisor>
    </aop:config>

 

  AOP 标签 , 使用增强 需要在 aop:config 节点中加入3个节点 

 aop:pointcut  aop:advisor(一个为前,一个为后)

 aop:pointcut 为使用增强的方法 ,这里路径说的是services下的所有方法,

 aop:advisor 这个为使用增强的,advice为增强bean的id, pointcot-ref为上面使用方法的id 

 下面为展示的测试类 

"C:\Program Files\Java\jdk1.8.0_91\bin\java" -ea -Didea.launcher.port=7533 "-Didea.launcher.bin.path=D:\mybaj\IntelliJ IDEA 2016.3.2\bin" -Dfile.encoding=UTF-8 -classpath "D:\mybaj\IntelliJ IDEA 2016.3.2\lib\idea_rt.jar;D:\mybaj\IntelliJ IDEA 2016.3.2\plugins\junit\lib\junit-rt.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\rt.jar;F:\Mybatis\01MyBatis\target\test-classes;F:\Mybatis\01MyBatis\target\classes;F:\Mybatis\lib\javax.annotation.jar;F:\Mybatis\lib\javax.jms.jar;F:\Mybatis\lib\javax.persistence.jar;F:\Mybatis\lib\javax.ejb.jar;F:\Mybatis\lib\javax.transaction.jar;F:\Mybatis\lib\javax.resource.jar;F:\Mybatis\lib\javax.servlet.jar;F:\Mybatis\lib\javax.servlet.jsp.jar;C:\Users\维吉的笔记本\.m2\repository\junit\junit\4.12\junit-4.12.jar;C:\Users\维吉的笔记本\.m2\repository\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar;C:\Users\维吉的笔记本\.m2\repository\mysql\mysql-connector-java\5.1.25\mysql-connector-java-5.1.25.jar;C:\Users\维吉的笔记本\.m2\repository\javax\servlet\jstl\1.2\jstl-1.2.jar;C:\Users\维吉的笔记本\.m2\repository\javaee\javaee-api\5\javaee-api-5.jar;C:\Users\维吉的笔记本\.m2\repository\com\alibaba\fastjson\1.2.41\fastjson-1.2.41.jar;C:\Users\维吉的笔记本\.m2\repository\org\mybatis\mybatis\3.4.1\mybatis-3.4.1.jar;C:\Users\维吉的笔记本\.m2\repository\org\slf4j\slf4j-api\1.7.21\slf4j-api-1.7.21.jar;C:\Users\维吉的笔记本\.m2\repository\ch\qos\logback\logback-core\1.2.3\logback-core-1.2.3.jar;C:\Users\维吉的笔记本\.m2\repository\org\springframework\spring-beans\4.2.0.RELEASE\spring-beans-4.2.0.RELEASE.jar;C:\Users\维吉的笔记本\.m2\repository\org\springframework\spring-core\4.2.0.RELEASE\spring-core-4.2.0.RELEASE.jar;C:\Users\维吉的笔记本\.m2\repository\commons-logging\commons-logging\1.2\commons-logging-1.2.jar;C:\Users\维吉的笔记本\.m2\repository\org\springframework\spring-context\4.2.0.RELEASE\spring-context-4.2.0.RELEASE.jar;C:\Users\维吉的笔记本\.m2\repository\org\springframework\spring-aop\4.2.0.RELEASE\spring-aop-4.2.0.RELEASE.jar;C:\Users\维吉的笔记本\.m2\repository\aopalliance\aopalliance\1.0\aopalliance-1.0.jar;C:\Users\维吉的笔记本\.m2\repository\org\springframework\spring-expression\4.2.0.RELEASE\spring-expression-4.2.0.RELEASE.jar;C:\Users\维吉的笔记本\.m2\repository\org\aspectj\aspectjweaver\1.8.7\aspectjweaver-1.8.7.jar;C:\Users\维吉的笔记本\.m2\repository\ch\qos\logback\logback-classic\1.2.3\logback-classic-1.2.3.jar" com.intellij.rt.execution.application.AppMain com.intellij.rt.execution.junit.JUnitStarter -ideVersion5 tt,tweo
三月 05, 2018 11:18:03 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@2280cdac: startup date [Mon Mar 05 23:18:03 CST 2018]; root of context hierarchy
三月 05, 2018 11:18:03 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [applicationContext02.xml]
bengin
输出
日志记录

Process finished with exit code 0

 

  这就是我前后置增强的输出 

 

下面来说构造注入 

通过构造和spring来为自己的属性赋值 

创建一个普通类  就写 学生吧 

名字 年龄  并且生成带参构造 

public class Student  {

    private String name;

    private Integer age;

    public  void info(){
        System.out.println(this.name);
        System.out.println(this.age);
    }

    public Student(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Student() {
    }
}

 

 配置文件中的输出 

 <bean id="stu" class="cn.study.dao.Student" scope="prototype">

        <constructor-arg index="0" value="x某"></constructor-arg>

        <constructor-arg index="1" value="18"></constructor-arg>
    </bean>

     1 对应带参构造的属性名顺序来赋值

     2 index为下标,从零开始      

     3 Value为属性值 

        测试类入下 

  public  void tweo(){
        ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext02.xml");
        Student jk=     (Student)context.getBean("stu");
        System.out.println(jk.getName());
        System.out.println(jk.getAge());
    }

 

 下面 发结果 

"C:\Program Files\Java\jdk1.8.0_91\bin\java" -ea -Didea.launcher.port=7535 "-Didea.launcher.bin.path=D:\mybaj\IntelliJ IDEA 2016.3.2\bin" -Dfile.encoding=UTF-8 -classpath "D:\mybaj\IntelliJ IDEA 2016.3.2\lib\idea_rt.jar;D:\mybaj\IntelliJ IDEA 2016.3.2\plugins\junit\lib\junit-rt.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\rt.jar;F:\Mybatis\01MyBatis\target\test-classes;F:\Mybatis\01MyBatis\target\classes;F:\Mybatis\lib\javax.annotation.jar;F:\Mybatis\lib\javax.jms.jar;F:\Mybatis\lib\javax.persistence.jar;F:\Mybatis\lib\javax.ejb.jar;F:\Mybatis\lib\javax.transaction.jar;F:\Mybatis\lib\javax.resource.jar;F:\Mybatis\lib\javax.servlet.jar;F:\Mybatis\lib\javax.servlet.jsp.jar;C:\Users\维吉的笔记本\.m2\repository\junit\junit\4.12\junit-4.12.jar;C:\Users\维吉的笔记本\.m2\repository\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar;C:\Users\维吉的笔记本\.m2\repository\mysql\mysql-connector-java\5.1.25\mysql-connector-java-5.1.25.jar;C:\Users\维吉的笔记本\.m2\repository\javax\servlet\jstl\1.2\jstl-1.2.jar;C:\Users\维吉的笔记本\.m2\repository\javaee\javaee-api\5\javaee-api-5.jar;C:\Users\维吉的笔记本\.m2\repository\com\alibaba\fastjson\1.2.41\fastjson-1.2.41.jar;C:\Users\维吉的笔记本\.m2\repository\org\mybatis\mybatis\3.4.1\mybatis-3.4.1.jar;C:\Users\维吉的笔记本\.m2\repository\org\slf4j\slf4j-api\1.7.21\slf4j-api-1.7.21.jar;C:\Users\维吉的笔记本\.m2\repository\ch\qos\logback\logback-core\1.2.3\logback-core-1.2.3.jar;C:\Users\维吉的笔记本\.m2\repository\org\springframework\spring-beans\4.2.0.RELEASE\spring-beans-4.2.0.RELEASE.jar;C:\Users\维吉的笔记本\.m2\repository\org\springframework\spring-core\4.2.0.RELEASE\spring-core-4.2.0.RELEASE.jar;C:\Users\维吉的笔记本\.m2\repository\commons-logging\commons-logging\1.2\commons-logging-1.2.jar;C:\Users\维吉的笔记本\.m2\repository\org\springframework\spring-context\4.2.0.RELEASE\spring-context-4.2.0.RELEASE.jar;C:\Users\维吉的笔记本\.m2\repository\org\springframework\spring-aop\4.2.0.RELEASE\spring-aop-4.2.0.RELEASE.jar;C:\Users\维吉的笔记本\.m2\repository\aopalliance\aopalliance\1.0\aopalliance-1.0.jar;C:\Users\维吉的笔记本\.m2\repository\org\springframework\spring-expression\4.2.0.RELEASE\spring-expression-4.2.0.RELEASE.jar;C:\Users\维吉的笔记本\.m2\repository\org\aspectj\aspectjweaver\1.8.7\aspectjweaver-1.8.7.jar;C:\Users\维吉的笔记本\.m2\repository\ch\qos\logback\logback-classic\1.2.3\logback-classic-1.2.3.jar" com.intellij.rt.execution.application.AppMain com.intellij.rt.execution.junit.JUnitStarter -ideVersion5 tt,tweo
三月 05, 2018 11:27:18 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@2280cdac: startup date [Mon Mar 05 23:27:18 CST 2018]; root of context hierarchy
三月 05, 2018 11:27:18 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [applicationContext02.xml]
x某
18

Process finished with exit code 0

 输出 在 配置文件中输出的值 ,这就是构造注入 

   下面来说 p命名空间注入

   还是拿上面的学生类来说 

   用p标签来注入 

   首先 ,在头文件中加一个 

xmlns:p="http://www.springframework.org/schema/p"

     之后便能使用了,

  写法如下 

 <bean id="su" class="cn.study.dao.Student" p:name="xx" p:age="18"></bean>

   之后是测试类 

    public  void tweo(){
        ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext02.xml");



       Student jk=     (Student)context.getBean("su");

        System.out.println(jk.getName());
        System.out.println(jk.getAge());
    }

   控制台 

"C:\Program Files\Java\jdk1.8.0_91\bin\java" -ea -Didea.launcher.port=7536 "-Didea.launcher.bin.path=D:\mybaj\IntelliJ IDEA 2016.3.2\bin" -Dfile.encoding=UTF-8 -classpath "D:\mybaj\IntelliJ IDEA 2016.3.2\lib\idea_rt.jar;D:\mybaj\IntelliJ IDEA 2016.3.2\plugins\junit\lib\junit-rt.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\rt.jar;F:\Mybatis\01MyBatis\target\test-classes;F:\Mybatis\01MyBatis\target\classes;F:\Mybatis\lib\javax.annotation.jar;F:\Mybatis\lib\javax.jms.jar;F:\Mybatis\lib\javax.persistence.jar;F:\Mybatis\lib\javax.ejb.jar;F:\Mybatis\lib\javax.transaction.jar;F:\Mybatis\lib\javax.resource.jar;F:\Mybatis\lib\javax.servlet.jar;F:\Mybatis\lib\javax.servlet.jsp.jar;C:\Users\维吉的笔记本\.m2\repository\junit\junit\4.12\junit-4.12.jar;C:\Users\维吉的笔记本\.m2\repository\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar;C:\Users\维吉的笔记本\.m2\repository\mysql\mysql-connector-java\5.1.25\mysql-connector-java-5.1.25.jar;C:\Users\维吉的笔记本\.m2\repository\javax\servlet\jstl\1.2\jstl-1.2.jar;C:\Users\维吉的笔记本\.m2\repository\javaee\javaee-api\5\javaee-api-5.jar;C:\Users\维吉的笔记本\.m2\repository\com\alibaba\fastjson\1.2.41\fastjson-1.2.41.jar;C:\Users\维吉的笔记本\.m2\repository\org\mybatis\mybatis\3.4.1\mybatis-3.4.1.jar;C:\Users\维吉的笔记本\.m2\repository\org\slf4j\slf4j-api\1.7.21\slf4j-api-1.7.21.jar;C:\Users\维吉的笔记本\.m2\repository\ch\qos\logback\logback-core\1.2.3\logback-core-1.2.3.jar;C:\Users\维吉的笔记本\.m2\repository\org\springframework\spring-beans\4.2.0.RELEASE\spring-beans-4.2.0.RELEASE.jar;C:\Users\维吉的笔记本\.m2\repository\org\springframework\spring-core\4.2.0.RELEASE\spring-core-4.2.0.RELEASE.jar;C:\Users\维吉的笔记本\.m2\repository\commons-logging\commons-logging\1.2\commons-logging-1.2.jar;C:\Users\维吉的笔记本\.m2\repository\org\springframework\spring-context\4.2.0.RELEASE\spring-context-4.2.0.RELEASE.jar;C:\Users\维吉的笔记本\.m2\repository\org\springframework\spring-aop\4.2.0.RELEASE\spring-aop-4.2.0.RELEASE.jar;C:\Users\维吉的笔记本\.m2\repository\aopalliance\aopalliance\1.0\aopalliance-1.0.jar;C:\Users\维吉的笔记本\.m2\repository\org\springframework\spring-expression\4.2.0.RELEASE\spring-expression-4.2.0.RELEASE.jar;C:\Users\维吉的笔记本\.m2\repository\org\aspectj\aspectjweaver\1.8.7\aspectjweaver-1.8.7.jar;C:\Users\维吉的笔记本\.m2\repository\ch\qos\logback\logback-classic\1.2.3\logback-classic-1.2.3.jar" com.intellij.rt.execution.application.AppMain com.intellij.rt.execution.junit.JUnitStarter -ideVersion5 tt,tweo
三月 05, 2018 11:34:06 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@2280cdac: startup date [Mon Mar 05 23:34:06 CST 2018]; root of context hierarchy
三月 05, 2018 11:34:06 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [applicationContext02.xml]
xx
18

Process finished with exit code 0

   输出的值与在bean里的赋值一样

 

   下面来说 集合属性的注入 

   还是先看 类 

    测试全为String类型  数组 List set Map  Properties

 

package cn.study.entity;



import java.util.*;

/**
 * Created by 维吉的笔记本 on 2018/3/5.
 */
public class Collcation {

    private  String[]  array;

    private List<String> list;

    private Set<String> set;

    private Map<String,String> map;

    private Properties properties;

    @Override
    public java.lang.String toString() {
        return "Collcation{" +
                "array=" + Arrays.toString(array) +
                ", list=" + list +
                ", set=" + set +
                ", map=" + map +
                ", properties=" + properties +
                '}';
    }

    public String[] getArray() {
        return array;
    }

    public void setArray(String[] array) {
        this.array = array;
    }

    public List<String> getList() {
        return list;
    }

    public void setList(List<String> list) {
        this.list = list;
    }

    public Set<String> getSet() {
        return set;
    }

    public void setSet(Set<String> set) {
        this.set = set;
    }

    public Map<String, String> getMap() {
        return map;
    }

    public void setMap(Map<String, String> map) {
        this.map = map;
    }

    public Properties getProperties() {
        return properties;
    }

    public void setProperties(Properties properties) {
        this.properties = properties;
    }
}

 

下面来说 具体的使用方法 

 

 <bean id="ms" class="cn.study.entity.Collcation">

        <property name="array">

            <array>
                <value>哈哈1</value>
                <value>哈哈2</value>
                <value>哈哈3</value>

            </array>

        </property>
        <property name="list">
            <list>

                <value>A先生</value>
                <value>B先生</value>
                <value>c先生</value>
            </list>

        </property>

        <property name="set">
            <set>
                <value>老板</value>
                <value>总裁</value>
                <value>董事长</value>

            </set>

        </property>

        <property name="map">
            <map>
                <entry key="a111">
                    <value>风景</value>
                </entry>
                <entry key="a222">
                    <value>山河</value>
                </entry>
                <entry key="a333">
                    <value>江河</value>
                </entry>
            </map>
        </property>



        <property name="properties">
            <props>
                <prop key="a1">11</prop>
                <prop key="a2">22</prop>
                <prop key="a3">33</prop>
            </props>

        </property>
    </bean>

     以上为赋值过程 , 下面就直接输出吧 , 这里赋值好像没有什么问题  

    接下来就是测试类了 

  public  void ffo(){
        ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext02.xml");
        Collcation c=    (Collcation)context.getBean("ms");

        System.out.println(c);


    }
"C:\Program Files\Java\jdk1.8.0_91\bin\java" -ea -Didea.launcher.port=7538 "-Didea.launcher.bin.path=D:\mybaj\IntelliJ IDEA 2016.3.2\bin" -Dfile.encoding=UTF-8 -classpath "D:\mybaj\IntelliJ IDEA 2016.3.2\lib\idea_rt.jar;D:\mybaj\IntelliJ IDEA 2016.3.2\plugins\junit\lib\junit-rt.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\rt.jar;F:\Mybatis\01MyBatis\target\test-classes;F:\Mybatis\01MyBatis\target\classes;F:\Mybatis\lib\javax.annotation.jar;F:\Mybatis\lib\javax.jms.jar;F:\Mybatis\lib\javax.persistence.jar;F:\Mybatis\lib\javax.ejb.jar;F:\Mybatis\lib\javax.transaction.jar;F:\Mybatis\lib\javax.resource.jar;F:\Mybatis\lib\javax.servlet.jar;F:\Mybatis\lib\javax.servlet.jsp.jar;C:\Users\维吉的笔记本\.m2\repository\junit\junit\4.12\junit-4.12.jar;C:\Users\维吉的笔记本\.m2\repository\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar;C:\Users\维吉的笔记本\.m2\repository\mysql\mysql-connector-java\5.1.25\mysql-connector-java-5.1.25.jar;C:\Users\维吉的笔记本\.m2\repository\javax\servlet\jstl\1.2\jstl-1.2.jar;C:\Users\维吉的笔记本\.m2\repository\javaee\javaee-api\5\javaee-api-5.jar;C:\Users\维吉的笔记本\.m2\repository\com\alibaba\fastjson\1.2.41\fastjson-1.2.41.jar;C:\Users\维吉的笔记本\.m2\repository\org\mybatis\mybatis\3.4.1\mybatis-3.4.1.jar;C:\Users\维吉的笔记本\.m2\repository\org\slf4j\slf4j-api\1.7.21\slf4j-api-1.7.21.jar;C:\Users\维吉的笔记本\.m2\repository\ch\qos\logback\logback-core\1.2.3\logback-core-1.2.3.jar;C:\Users\维吉的笔记本\.m2\repository\org\springframework\spring-beans\4.2.0.RELEASE\spring-beans-4.2.0.RELEASE.jar;C:\Users\维吉的笔记本\.m2\repository\org\springframework\spring-core\4.2.0.RELEASE\spring-core-4.2.0.RELEASE.jar;C:\Users\维吉的笔记本\.m2\repository\commons-logging\commons-logging\1.2\commons-logging-1.2.jar;C:\Users\维吉的笔记本\.m2\repository\org\springframework\spring-context\4.2.0.RELEASE\spring-context-4.2.0.RELEASE.jar;C:\Users\维吉的笔记本\.m2\repository\org\springframework\spring-aop\4.2.0.RELEASE\spring-aop-4.2.0.RELEASE.jar;C:\Users\维吉的笔记本\.m2\repository\aopalliance\aopalliance\1.0\aopalliance-1.0.jar;C:\Users\维吉的笔记本\.m2\repository\org\springframework\spring-expression\4.2.0.RELEASE\spring-expression-4.2.0.RELEASE.jar;C:\Users\维吉的笔记本\.m2\repository\org\aspectj\aspectjweaver\1.8.7\aspectjweaver-1.8.7.jar;C:\Users\维吉的笔记本\.m2\repository\ch\qos\logback\logback-classic\1.2.3\logback-classic-1.2.3.jar" com.intellij.rt.execution.application.AppMain com.intellij.rt.execution.junit.JUnitStarter -ideVersion5 tt,ffo
三月 05, 2018 11:45:34 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@2280cdac: startup date [Mon Mar 05 23:45:34 CST 2018]; root of context hierarchy
三月 05, 2018 11:45:34 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [applicationContext02.xml]
Collcation{array=[哈哈1, 哈哈2, 哈哈3], list=[A先生, B先生, C先生], set=[老板, 总裁, 董事长], map={a111=风景, a222=山河, a333=江河}, properties={a2=22, a1=11, a3=33}}

Process finished with exit code 0

   以下为输出  会把我们在 bean里所赋的值给输出 

    

下面介绍 

单例:Singleton
多例:prototype

我们都知道,我们在使用配置的时候,都会new一下bean里,会实例化,上一篇博客已经说过了 

 使用方法 在 是  bean 里的 属性 加一个 scope,

scope 的值为上面的单例和双例  

单例就是 用两遍相同的bean ,他只会实例化一次

 

    <bean id="stu" class="cn.study.dao.Student" scope="singleton">

        <constructor-arg index="0" value="x某"></constructor-arg>

        <constructor-arg index="1" value="18"></constructor-arg>
    </bean>

  

  public  void tweo(){
        ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext02.xml");

//        Helloseviec hh=  (Helloseviec)context.getBean("service");
//
//        hh.some();

       Student jk=     (Student)context.getBean("stu");
        Student j=     (Student)context.getBean("stu");
        System.out.println(jk);
        System.out.println(j);
        System.out.println(j.getName());
        System.out.println(j.getAge());
        System.out.println(jk.getName());
        System.out.println(jk.getAge());
    }
"C:\Program Files\Java\jdk1.8.0_91\bin\java" -ea -Didea.launcher.port=7534 "-Didea.launcher.bin.path=D:\mybaj\IntelliJ IDEA 2016.3.2\bin" -Dfile.encoding=UTF-8 -classpath "D:\mybaj\IntelliJ IDEA 2016.3.2\lib\idea_rt.jar;D:\mybaj\IntelliJ IDEA 2016.3.2\plugins\junit\lib\junit-rt.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\rt.jar;F:\Mybatis\01MyBatis\target\test-classes;F:\Mybatis\01MyBatis\target\classes;F:\Mybatis\lib\javax.annotation.jar;F:\Mybatis\lib\javax.jms.jar;F:\Mybatis\lib\javax.persistence.jar;F:\Mybatis\lib\javax.ejb.jar;F:\Mybatis\lib\javax.transaction.jar;F:\Mybatis\lib\javax.resource.jar;F:\Mybatis\lib\javax.servlet.jar;F:\Mybatis\lib\javax.servlet.jsp.jar;C:\Users\维吉的笔记本\.m2\repository\junit\junit\4.12\junit-4.12.jar;C:\Users\维吉的笔记本\.m2\repository\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar;C:\Users\维吉的笔记本\.m2\repository\mysql\mysql-connector-java\5.1.25\mysql-connector-java-5.1.25.jar;C:\Users\维吉的笔记本\.m2\repository\javax\servlet\jstl\1.2\jstl-1.2.jar;C:\Users\维吉的笔记本\.m2\repository\javaee\javaee-api\5\javaee-api-5.jar;C:\Users\维吉的笔记本\.m2\repository\com\alibaba\fastjson\1.2.41\fastjson-1.2.41.jar;C:\Users\维吉的笔记本\.m2\repository\org\mybatis\mybatis\3.4.1\mybatis-3.4.1.jar;C:\Users\维吉的笔记本\.m2\repository\org\slf4j\slf4j-api\1.7.21\slf4j-api-1.7.21.jar;C:\Users\维吉的笔记本\.m2\repository\ch\qos\logback\logback-core\1.2.3\logback-core-1.2.3.jar;C:\Users\维吉的笔记本\.m2\repository\org\springframework\spring-beans\4.2.0.RELEASE\spring-beans-4.2.0.RELEASE.jar;C:\Users\维吉的笔记本\.m2\repository\org\springframework\spring-core\4.2.0.RELEASE\spring-core-4.2.0.RELEASE.jar;C:\Users\维吉的笔记本\.m2\repository\commons-logging\commons-logging\1.2\commons-logging-1.2.jar;C:\Users\维吉的笔记本\.m2\repository\org\springframework\spring-context\4.2.0.RELEASE\spring-context-4.2.0.RELEASE.jar;C:\Users\维吉的笔记本\.m2\repository\org\springframework\spring-aop\4.2.0.RELEASE\spring-aop-4.2.0.RELEASE.jar;C:\Users\维吉的笔记本\.m2\repository\aopalliance\aopalliance\1.0\aopalliance-1.0.jar;C:\Users\维吉的笔记本\.m2\repository\org\springframework\spring-expression\4.2.0.RELEASE\spring-expression-4.2.0.RELEASE.jar;C:\Users\维吉的笔记本\.m2\repository\org\aspectj\aspectjweaver\1.8.7\aspectjweaver-1.8.7.jar;C:\Users\维吉的笔记本\.m2\repository\ch\qos\logback\logback-classic\1.2.3\logback-classic-1.2.3.jar" com.intellij.rt.execution.application.AppMain com.intellij.rt.execution.junit.JUnitStarter -ideVersion5 tt,tweo
三月 05, 2018 11:56:50 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@2280cdac: startup date [Mon Mar 05 23:56:50 CST 2018]; root of context hierarchy
三月 05, 2018 11:56:50 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [applicationContext02.xml]
cn.study.dao.Student@4d9e68d0
cn.study.dao.Student@4d9e68d0
x某
18
x某
18

Process finished with exit code 0

两个相同的地址 ,改变scope ,为双例 

 

 

 <bean id="stu" class="cn.study.dao.Student" scope="prototype">

        <constructor-arg index="0" value="x某"></constructor-arg>

        <constructor-arg index="1" value="18"></constructor-arg>
    </bean>
"C:\Program Files\Java\jdk1.8.0_91\bin\java" -ea -Didea.launcher.port=7535 "-Didea.launcher.bin.path=D:\mybaj\IntelliJ IDEA 2016.3.2\bin" -Dfile.encoding=UTF-8 -classpath "D:\mybaj\IntelliJ IDEA 2016.3.2\lib\idea_rt.jar;D:\mybaj\IntelliJ IDEA 2016.3.2\plugins\junit\lib\junit-rt.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\rt.jar;F:\Mybatis\01MyBatis\target\test-classes;F:\Mybatis\01MyBatis\target\classes;F:\Mybatis\lib\javax.annotation.jar;F:\Mybatis\lib\javax.jms.jar;F:\Mybatis\lib\javax.persistence.jar;F:\Mybatis\lib\javax.ejb.jar;F:\Mybatis\lib\javax.transaction.jar;F:\Mybatis\lib\javax.resource.jar;F:\Mybatis\lib\javax.servlet.jar;F:\Mybatis\lib\javax.servlet.jsp.jar;C:\Users\维吉的笔记本\.m2\repository\junit\junit\4.12\junit-4.12.jar;C:\Users\维吉的笔记本\.m2\repository\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar;C:\Users\维吉的笔记本\.m2\repository\mysql\mysql-connector-java\5.1.25\mysql-connector-java-5.1.25.jar;C:\Users\维吉的笔记本\.m2\repository\javax\servlet\jstl\1.2\jstl-1.2.jar;C:\Users\维吉的笔记本\.m2\repository\javaee\javaee-api\5\javaee-api-5.jar;C:\Users\维吉的笔记本\.m2\repository\com\alibaba\fastjson\1.2.41\fastjson-1.2.41.jar;C:\Users\维吉的笔记本\.m2\repository\org\mybatis\mybatis\3.4.1\mybatis-3.4.1.jar;C:\Users\维吉的笔记本\.m2\repository\org\slf4j\slf4j-api\1.7.21\slf4j-api-1.7.21.jar;C:\Users\维吉的笔记本\.m2\repository\ch\qos\logback\logback-core\1.2.3\logback-core-1.2.3.jar;C:\Users\维吉的笔记本\.m2\repository\org\springframework\spring-beans\4.2.0.RELEASE\spring-beans-4.2.0.RELEASE.jar;C:\Users\维吉的笔记本\.m2\repository\org\springframework\spring-core\4.2.0.RELEASE\spring-core-4.2.0.RELEASE.jar;C:\Users\维吉的笔记本\.m2\repository\commons-logging\commons-logging\1.2\commons-logging-1.2.jar;C:\Users\维吉的笔记本\.m2\repository\org\springframework\spring-context\4.2.0.RELEASE\spring-context-4.2.0.RELEASE.jar;C:\Users\维吉的笔记本\.m2\repository\org\springframework\spring-aop\4.2.0.RELEASE\spring-aop-4.2.0.RELEASE.jar;C:\Users\维吉的笔记本\.m2\repository\aopalliance\aopalliance\1.0\aopalliance-1.0.jar;C:\Users\维吉的笔记本\.m2\repository\org\springframework\spring-expression\4.2.0.RELEASE\spring-expression-4.2.0.RELEASE.jar;C:\Users\维吉的笔记本\.m2\repository\org\aspectj\aspectjweaver\1.8.7\aspectjweaver-1.8.7.jar;C:\Users\维吉的笔记本\.m2\repository\ch\qos\logback\logback-classic\1.2.3\logback-classic-1.2.3.jar" com.intellij.rt.execution.application.AppMain com.intellij.rt.execution.junit.JUnitStarter -ideVersion5 tt,tweo
三月 05, 2018 11:59:26 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@2280cdac: startup date [Mon Mar 05 23:59:26 CST 2018]; root of context hierarchy
三月 05, 2018 11:59:26 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [applicationContext02.xml]
cn.study.dao.Student@14dd9eb7
cn.study.dao.Student@52e6fdee
x某
18
x某
18

 

 两块不同的地址 , 这就是单例和双例了

 

 

 

posted on 2018-03-06 00:01  LWJDD  阅读(154)  评论(0编辑  收藏  举报

导航