Spring 中 context:property-placeholder @Bean

 

Spring中 context:property-placeholder 元素

 

<bean id="propertyPlaceholderConfigurer" 
class
="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>userinfo.properties</value> </list> </property> </bean> <bean name="userInfo" class="test.UserInfo"> <property name="username" value="${db.username}"/> <property name="password" value="${db.password}"/> </bean>

Spring提供了<context:property-placeholder/>元素。下面给出了配置示例,启用它后,开发者便不用配置PropertyPlaceholderConfigurer对象了。

<context:property-placeholder location="userinfo.properties"/>

 

Spring @Bean


该注释的属性的名称和语义类似于Spring XML模式中bean的元素的名称和语义。@Bean指示方法产生一个由Spring容器管理的bean。
@Bean可以用在方法注释上。

@Configuration
public class DataBaseConfig {
    @Bean("dataSource")
    public DataSource getDataSource(){
        DataSource dataSource = new DataSource();
        dataSource.setUserId("jingsi");
        dataSource.setPassword("123456");
        dataSource.setUrl("www");
        return dataSource;
    }

}

@Bean明确地指示了一种方法,什么方法呢——产生一个bean的方法,并且交给Spring容器管理;

@Bean 用在方法上,告诉Spring容器,你可以从下面这个方法中拿到一个Bean

 示例:

@Service
public class BeanTest {  

    @Bean
    public   BeanTest  getBean(){
        BeanTest bean = new  BeanTest();
        System.out.println("调用方法:"+bean);
        return bean;
    }
}

public class Main {
    @SuppressWarnings("unused")
    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("application-context.xml");

        Object bean1 = context.getBean("getBean");
        System.out.println(bean1);
        Object bean2 = context.getBean("getBean");
        System.out.println(bean2);
    }
}

输出:

调用方法:Spring.BeanTest@5a4041cc
Spring.BeanTest@5a4041cc
Spring.BeanTest@5a4041cc

默认bean的名称就是其方法名

 

@Bean可以为Bean同时定义多个别名,但是别名不能为空字符串。

@Configuration
public class DataBaseConfig {
    @Bean({"dataSource","data2"})
    public DataSource getDataSource(){
        DataSource dataSource = new DataSource();
        dataSource.setUserId("jingsi");
        dataSource.setPassword("123456");
        dataSource.setUrl("www");
        return dataSource;
    }

}

 

接受生命周期的回调

任何使用@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、通过@CompentScan +@Controller @Service @Respository @compent

适合场景:我们自己写的组件可以通过这种方式来进行加载到容器中。

2、通过@Bean的方式来导入组件

适合场景: 适用于导入第三方组件的类

3、通过@Import 来导入组件

适合场景:导入组件的id为全路径,用处最多的是其他框架整个Spring时,使用@Import注解导入整合类。

演示@Import注解 的使用

1、注解导入需要初始化的类

@Import (value = {Person.class, Car.class})

2、实现 ImportSelector 接口,在接口中返回id的全路径

public class HelloImportSelector implements ImportSelector {
    @Override
    public String[] selectImports(AnnotationMetadata importingClassMetadata) {
        return new String[]{"com.xx.testimport.compent.Person"};
    }
}
@Configuration
@Import(value = {HelloImportSelector.class})
@ComponentScan(basePackages ={"com.xx.testimport.scanner"})
public class MainConfig {
} 

运行后Person就会被加载到Spring容器中。

 

REF
https://www.cnblogs.com/feiyu127/p/7700090.html
https://www.zhihu.com/question/428542278/answer/1645069838
https://www.jianshu.com/p/0628474b4cfa
https://blog.csdn.net/mapleleafforest/article/details/86623578

https://www.jianshu.com/p/2f904bebb9d0

posted @ 2021-12-24 13:04  emanlee  阅读(251)  评论(0编辑  收藏  举报