spring容器

XML方式&註解方式java

pom.xmlweb

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.xp.cn</groupId>
    <artifactId>spring</artifactId>
    <version>1.0-SNAPSHOT</version>

    <name>spring</name>
    <!-- FIXME change it to the project's website -->
    <url>http://www.example.com</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
    </properties>

    <dependencies>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.3.12.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.18</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>

    </build>
</project>

 

1.xml配置Beanspring

<?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">


   <!-- 包掃描、只要標註了@Controller、@Service、@Repository,@Component -->
    <!-- <context:component-scan base-package="com.atguigu" use-default-filters="false"></context:component-scan> -->
   <bean id="person" class="com.xp.cn.model.Person" scope="prototype" >
      <property name="age" value="18"></property>
      <property name="name" value="zhangsan"></property>
   </bean>
   
   
</beans>

 

2.註解配置Beanapache

package com.xp.cn.config;

import com.xp.cn.model.Person;
import org.springframework.context.annotation.*;

//配置類等於之前的配置文件
@Configuration //告訴spring這個是一個配置類
//@ComponentScan(value = "com.xp",includeFilters = {//value:指定要掃描的包
//        @ComponentScan.Filter(type= FilterType.ANNOTATION,classes = {Controller.class, Service.class})
//},useDefaultFilters = false)//useDefaultFilters = true,默認爲true,掃描所有註解



//@ComponentScan(value = "com.xp",excludeFilters = {//value:指定要掃描的包
//        @ComponentScan.Filter(type= FilterType.ANNOTATION,classes = {Controller.class, Service.class})
//})//useDefaultFilters = true,默認爲true,掃描所有註解



//@ComponentScans(value = {//指定多個掃描規則
//        @ComponentScan(value = "com.xp", excludeFilters = {//value:指定要掃描的包
//                @ComponentScan.Filter(type = FilterType.ANNOTATION, classes = {Controller.class, Service.class})
//        })//useDefaultFilters = true,默認爲true,掃描所有註解
//})

//@ComponentScans(value = {//指定多個掃描規則
//        @ComponentScan(value = "com.xp", includeFilters = {//value:指定要掃描的包
//                @ComponentScan.Filter(type = FilterType.ANNOTATION, classes = {Controller.class, Service.class}),
//                @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE,classes = {BookDao.class}),
//                @ComponentScan.Filter(type = FilterType.CUSTOM,classes = {MyTypeFilter.class})
//        },useDefaultFilters = false)//useDefaultFilters = true,默認爲true,掃描所有註解
//})
@ComponentScans(value = {//指定多個掃描規則
        @ComponentScan(value = "com.xp", includeFilters = {//value:指定要掃描的包
                @ComponentScan.Filter(type = FilterType.CUSTOM,classes = {MyTypeFilter.class})
        },useDefaultFilters = false)//useDefaultFilters = true,默認爲true,掃描所有註解
})


//FilterType.ANNOTATION : 按照註解掃描
//FilterType.TYPE : 按照類型
//FilterType.ASPECTJ,
//FilterType.REGEX,
//FilterType.CUSTOM;
public class MainConfig2 {

    //給容器中註冊一個Bean,類型是返回值的類型Person,id默認是方法名
    @Bean(value = "person002")//指定bean的名字
//    @Scope(value = "prototype")
//    @Lazy
    public Person person002() {
        System.out.println("建立對象");
        return new Person("wangwu", 32);
    }
}

 

package com.xp.cn.config;

import com.xp.cn.dao.BookDao;
import com.xp.cn.model.Person;
import com.xp.cn.service.BookService;
import org.springframework.context.annotation.*;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import sun.awt.SunHints;

import java.awt.print.Book;
import java.lang.reflect.Type;
import java.util.logging.Filter;

//配置類等於之前的配置文件
@Configuration //告訴spring這個是一個配置類
//@ComponentScan(value = "com.xp",includeFilters = {//value:指定要掃描的包
//        @ComponentScan.Filter(type= FilterType.ANNOTATION,classes = {Controller.class, Service.class})
//},useDefaultFilters = false)//useDefaultFilters = true,默認爲true,掃描所有註解



//@ComponentScan(value = "com.xp",excludeFilters = {//value:指定要掃描的包
//        @ComponentScan.Filter(type= FilterType.ANNOTATION,classes = {Controller.class, Service.class})
//})//useDefaultFilters = true,默認爲true,掃描所有註解



//@ComponentScans(value = {//指定多個掃描規則
//        @ComponentScan(value = "com.xp", excludeFilters = {//value:指定要掃描的包
//                @ComponentScan.Filter(type = FilterType.ANNOTATION, classes = {Controller.class, Service.class})
//        })//useDefaultFilters = true,默認爲true,掃描所有註解
//})

//@ComponentScans(value = {//指定多個掃描規則
//        @ComponentScan(value = "com.xp", includeFilters = {//value:指定要掃描的包
//                @ComponentScan.Filter(type = FilterType.ANNOTATION, classes = {Controller.class, Service.class}),
//                @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE,classes = {BookDao.class}),
//                @ComponentScan.Filter(type = FilterType.CUSTOM,classes = {MyTypeFilter.class})
//        },useDefaultFilters = false)//useDefaultFilters = true,默認爲true,掃描所有註解
//})
@ComponentScans(value = {//指定多個掃描規則
        @ComponentScan(value = "com.xp", includeFilters = {//value:指定要掃描的包
                @ComponentScan.Filter(type = FilterType.CUSTOM,classes = {MyTypeFilter.class})
        },useDefaultFilters = false)//useDefaultFilters = true,默認爲true,掃描所有註解
})


//FilterType.ANNOTATION : 按照註解掃描
//FilterType.TYPE : 按照類型
//FilterType.ASPECTJ,
//FilterType.REGEX,
//FilterType.CUSTOM;
public class MainConfig {

    //給容器中註冊一個Bean,類型是返回值的類型Person,id默認是方法名
    @Bean(value = "person001")//指定bean的名字
    public Person person001() {
        return new Person("wangwu", 12);
    }
}

 

xml和註解配置方式等價,配置更加方便快捷maven

package com.xp.cn.model;

import lombok.*;

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class Person {

    private String name;
    private Integer age;
    
}

 

獲取Beanide

package com.xp.cn;

import com.xp.cn.model.Person;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


public class App {
    public static void main(String[] args){

        //配置文件方式,ClassPathXmlApplicationContext參數爲配置文件的位置
        ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("beans.xml");
        Person person = (Person)classPathXmlApplicationContext.getBean("person");
        System.out.println(person);

        //註解方式,AnnotationConfigApplicationContext參數是配置類的位置
        AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(MainConfig.class);
        Person person1 = (Person)annotationConfigApplicationContext.getBean(Person.class);//更具類型獲取
        System.out.println(person1);

        //獲取bean的名字
        String[] beanNamesForType = annotationConfigApplicationContext.getBeanNamesForType(Person.class);//
        for (String name : beanNamesForType){
            System.out.println(name);
        }

    }
}

 

 

測試類:測試

package com.xp.cn;

import com.xp.cn.config.MainConfig;
import com.xp.cn.config.MainConfig2;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class IOCTest {

    @Test
    public void test1(){
        AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(MainConfig.class);
        String[] beanDefinitionNames = annotationConfigApplicationContext.getBeanDefinitionNames();
        for (String beanName : beanDefinitionNames){
            System.out.println(beanName);
        }

    }

    @Test
    public void test2(){
        AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(MainConfig2.class);
        String[] beanDefinitionNames = annotationConfigApplicationContext.getBeanDefinitionNames();
        for (String beanName : beanDefinitionNames){
            System.out.println(beanName);
        }

        Object person = annotationConfigApplicationContext.getBean("person002");
        Object person2 = annotationConfigApplicationContext.getBean("person002");
        System.out.println(person == person2);

    }



}
相關文章
相關標籤/搜索