spring-基于java的配置

java配置类

java提供一种基于java代码的方式完全替代xml配置。

java中只有类,那我们把一个类映射成xml就可以了。这也符合面向对象的思想。

java配置可以和xml配置配合使用,也可以完全废弃xml。

// @Configuration告知spring,该类下将被定义一个或多个Bean。
@Configuration	//@Component
public class SpringConfig {

    public SpringConfig() {
        System.out.println("spring config..");
    }

    //@Bean在spring上下文中注册了一个Bean,方法名为bean的id。
    //因为spring默认单例模式,因此该方法一开始就会且只会被调用一次。
    @Bean
    public Person p(){
        return new Person();
    }

    @Bean
    public Dog dog(){
        Dog dog = new Dog();
        dog.setName("dog wangwang..");
        return dog;
    }

    @Bean
//    @Scope(value = "prototype")
    public Animal animal(){
        System.out.println("animal run..");
        return new Animal("animal..");
    }

}


ApplicationContext applicationContext = new ClassPathXmlApplicationContext("/spring.xml");
        Person person = (Person)applicationContext.getBean("p");
        System.out.println(person.getName());

输出:

spring config..
person run...
human


1.spring上下文加载xml配置。

2.扫描xml配置的package。

3.注册@Bean到上下文中。


看源码可知@Configuration是一个@Component。因此我们可以尝试配置用直接用@Conponent注解。

package org.springframework.context.annotation;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.stereotype.Component;

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Configuration {
    String value() default "";
}


//@Configuration告知spring,该类下将被定义一个或多个Bean。
//@Configuration
@Component
public class SpringConfig {

    public SpringConfig() {
        System.out.println("spring config..");
    }

    //@Bean在spring上下文中注册了一个Bean,方法名为bean的id。
    //因为spring默认单例模式,因此该方法一开始就会且只会被调用一次。
    //方法中所有的代码都是为了构建一个合法的Bean返回。因此依赖关系我们也必须在代码中手动构p建。
    @Bean
    public Person p(){
        System.out.println("person run...");
        return new Person();
    }

    @Bean
    public Dog dog(){
//        System.out.println("dog run..");
        Dog dog = new Dog();
        return dog;
    }

    @Bean
//    @Scope(value = "prototype")
    public Animal animal(){
//        System.out.println("animal run..");
        return new Animal("animal..");
    }

}


正常输出:

spring config..
person run...
human


@Configuration和@Conponent的区别请参照。

http://blog.csdn.net/ttjxtjx/article/details/49866011


基于java配置的上下文

我们也可以完全基于java代码,只需要将原来的应用上下文由

ClassPathXmlApplicationContext
换为
AnnotationConfigApplicationContext
默认构造函数的值为需要扫描的package,就是
 
 
<context:component-scan base-package="demo.*"/>

中的demo.*。
 
 
或者直接将配置类作为参数,如下。如果是直接进行配置类注册的话,那么我们的配置类上面不需要任何的注解。反之如果是扫包的形式就需要注解。

AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
        Person p1 = (Person)context.getBean("p");
        System.out.println(p1.getName());
        System.out.println(p1.getPet().getName());

输出:

spring config..
animal run..
human
dog wangwang.


这里有的人会问为什么我在p方法中没有set相关的属性,而输出却是有值的呢?方法中我们只是new了,当时是肯定是没有值得,而spring获取到方法返回的对象后没有就此罢手,而是对这个bean作了一些手脚。根据我们的注解。

下面是Person类:

//@Component注解的bean被spring扫描到了之后自动纳入spring管理。
//@Component
public class Person {

    //通过spring的@Value动态注入属性值
    @Value("human")
    String name;

//    @Value("18")
    int age = 18;

    //自动注入,建立bean之间的依赖关系
    @Autowired
    Dog pet;

//    public Person(Dog pet,String name, int age) {
//        this.name = name;
//        this.age = age;
//        this.pet = pet;
//    }

    public Dog getPet() {
        return pet;
    }

    public void setPet(Dog pet) {
        this.pet = pet;
    }

    public int getAge() {
        return age;
    }

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

    public String getName() {
        return name;
    }

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

}


配置类之间的依赖

这样看是消除了刺眼的尖括号,然而实际上,这个陪之类依旧定了上千个bean,乱糟糟一片。

我们自然的就像给这个方法归类,比如说,dao层的Bean都放到一起,service层的放到一起,entity放在一起分门归类,形成多个配置类。

然后在引用上下中注册多次配置类。比如下面这样。


//@Configuration告知spring,该类下将被定义一个或多个Bean。
public class SpringConfig {

    public SpringConfig() {
        System.out.println("spring config..");
    }

    //@Bean在spring上下文中注册了一个Bean,方法名为bean的id。
    //因为spring默认单例模式,因此该方法一开始就会且只会被调用一次。
    //方法中所有的代码都是为了构建一个合法的Bean返回。因此依赖关系我们也必须在代码中手动构建。
    @Bean
    public Person p(){
        return new Person();
    }

    @Bean
    public Dog dog(){
        Dog dog = new Dog();
        dog.setName("dog wangwang..");
        return dog;
    }

    @Bean
//    @Scope(value = "prototype")
    public Animal animal(){
        System.out.println("animal run..");
        return new Animal("animal..");
    }

}


public class Data {

    @Value("root")
    private String user;

    @Value("123456")
    private String pwd;

    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }

    public String getPwd() {
        return pwd;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }
}

//@Configuration
public class OtherConfig {

    @Bean
    public Data data() {
        return new Data();
    }

}

AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
        context.register(SpringConfig.class);
        context.register(OtherConfig.class);
        context.refresh();
        Person p1 = (Person)context.getBean("p");
        System.out.println(p1.getName());
        System.out.println(p1.getPet().getName());
        Data data = (Data)context.getBean("data");
        System.out.println(data.getUser());

输出

spring config..
animal run..
human
dog wangwang..
root

这里注意在应用上下文中注册配置类的时候要调用refresh()方法。


这样看起来确实清爽了很多,然而做一件事情就要做到最好。

spring3.0提供@Import注解可以让我们为配置文件之类建立一种依赖关系。

如下代码。

//@Configuration
@Import({SpringConfig.class,OtherConfig.class})
public class AppConfig {
}


AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
        Person p1 = (Person)context.getBean("p");
        System.out.println(p1.getName());
        System.out.println(p1.getPet().getName());
        Data data = (Data)context.getBean("data");
        System.out.println(data.getUser());

ok,应用上下文现在只需要注册AppConfig就可以了。

猜你喜欢

转载自blog.csdn.net/helianus/article/details/78662747