spring bean注解使用详解

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ZYC88888/article/details/84237424

@Bean 的用法

@Bean是一个方法级别上的注解主要用在@Configuration注解的类里也可以用在@Component注解的类里。添加的bean的id为方法名

定义bean

下面是@Configuration里的一个例子


@Configuration

public class AppConfig {


@Bean

public TransferService transferService() {

return new TransferServiceImpl();

}


}

这个配置就等同于之前在xml里的配置


<beans>

<bean id="transferService" class="com.acme.TransferServiceImpl"/>

</beans>

bean的依赖

@bean 也可以依赖其他任意数量的bean,如果TransferService 依赖 AccountRepository,我们可以通过方法参数实现这个依赖


@Configuration

public class AppConfig {


@Bean

public TransferService transferService(AccountRepository accountRepository) {

return new TransferServiceImpl(accountRepository);

}


}

接受生命周期的回调

任何使用@Bean定义的bean,也可以执行生命周期的回调函数,类似@PostConstruct and @PreDestroy的方法。用法如下



public class Foo {

public void init() {

// initialization logic

}

}


public class Bar {

public void cleanup() {

// destruction logic

}

}


@Configuration

public class AppConfig {


@Bean(initMethod = "init")

public Foo foo() {

return new Foo();

}


@Bean(destroyMethod = "cleanup")

public Bar bar() {

return new Bar();

}


}

默认使用javaConfig配置的bean,如果存在close或者shutdown方法,则在bean销毁时会自动执行该方法,如果你不想执行该方法,则添加@Bean(destroyMethod="")来防止出发销毁方法

指定bean的scope

使用@Scope注解

你能够使用@Scope注解来指定使用@Bean定义的bean


@Configuration

public class MyConfiguration {


@Bean

@Scope("prototype")

public Encryptor encryptor() {

// ...

}


}

@Scope and scoped-proxy

spring提供了scope的代理,可以设置@Scope的属性proxyMode来指定,默认是ScopedProxyMode.NO, 你可以指定为默认是ScopedProxyMode.INTERFACES或者默认是ScopedProxyMode.TARGET_CLASS。
以下是一个demo



// an HTTP Session-scoped bean exposed as a proxy

@Bean

@SessionScope

public UserPreferences userPreferences() {

return new UserPreferences();

}


@Bean

public Service userService() {

UserService service = new SimpleUserService();

// a reference to the proxied userPreferences bean

service.setUserPreferences(userPreferences());

return service;

}

自定义bean的命名

默认情况下bean的名称和方法名称相同,你也可以使用name属性来指定


@Configuration

public class AppConfig {


@Bean(name = "myFoo")

public Foo foo() {

return new Foo();

}


}

bean的别名

bean的命名支持别名,使用方法如下


@Configuration

public class AppConfig {


@Bean(name = { "dataSource", "subsystemA-dataSource", "subsystemB-dataSource" })

public DataSource dataSource() {

// instantiate, configure and return DataSource bean...

}


}

bean的描述

有时候提供bean的详细信息也是很有用的,bean的描述可以使用 @Description来提供


@Configuration

public class AppConfig {


@Bean

@Description("Provides a basic example of a bean")

public Foo foo() {

return new Foo();

}


注: 
(1)、@Bean注解在返回实例的方法上,如果未通过@Bean指定bean的名称,则默认与标注的方法名相同; 

(2)、@Bean注解默认作用域为单例singleton作用域,可通过@Scope(“prototype”)设置为原型作用域; 

(3)、既然@Bean的作用是注册bean对象,那么完全可以使用@Component、@Controller、@Service、@Ripository等注解注册bean,当然需要配置@ComponentScan注解进行自动扫描。

bean类:

package com.test.spring.support.configuration;

//添加注册bean的注解
@Component
public class TestBean {

    public void sayHello(){
        System.out.println("TestBean sayHello...");
    }

    public String toString(){
        return "username:"+this.username+",url:"+this.url+",password:"+this.password;
    }
}


配置类:

@Configuration//添加自动扫描注解,basePackages为TestBean包路径

@ComponentScan(basePackages = "com.test.spring.support.configuration")
public class TestConfiguration {
    public TestConfiguration(){
        System.out.println("spring容器启动初始化。。。");
    }

    //取消@Bean注解注册bean的方式
    //@Bean
    //@Scope("prototype")
    //public TestBean testBean() {
    //  return new TestBean();
    //}
}


主方法测试获取bean对象:

public class TestMain {
    public static void main(String[] args) {
        ApplicationContext context = new AnnotationConfigApplicationContext(TestConfiguration.class);
        //获取bean
        TestBean tb = context.getBean("testBean");
        tb.sayHello();
    }
}



sayHello()方法都被正常调用。
 

Spring 声明Bean的注解:

@Component: 组件,没有明确的角色。 
@Service : 在业务逻辑层(Service层)使用。
@Repository:  再数据访问层(Dao层)使用。
@Controller: 再展现层(MVC->Spring MVC)使用。

Spring 注入Bean的注解:
@Autowired:Spring提供的注解。
@inject:JSR-330提供的注解。
@Resource:JSP-250提供的注解。

‘@Autowired’ 和‘@Inject’他们都是通过‘AutowiredAnnotationBeanPostProcessor’ 类实现的依赖注入,二者具有可互换性。 
‘@Resource’通过 ‘CommonAnnotationBeanPostProcessor’ 类实现依赖注入,即便如此他们在依赖注入时的表现还是极为相近的。
以下是他们在实现依赖注入时执行顺序的概括:

@Autowired and @Inject
Matches by Type
Restricts by Qualifiers
Matches by Name
@Resource
Matches by Name
Matches by Type
Restricts by Qualifiers (ignored if match is found by name)
参考:
http://blog.csdn.net/u013474104/article/details/44352765/
 

猜你喜欢

转载自blog.csdn.net/ZYC88888/article/details/84237424