栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java > SpringBoot

Spring基础使用(二)-------Bean的注解方式装配

SpringBoot 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring基础使用(二)-------Bean的注解方式装配

1、常用注解

  • @Component:通用注解
  • @Repository:一般用于注解持久化层的类
  • @Service:一般用于注解Service类
  • @Controller:一般用于注解控制器类。
  • @Named:与@Component用法相同。

  • @Autowired:注入对象,用于修饰成员,set方法和构造方法。
  • @Resource:与@Autowired用法相同
  • @Inject:与@Autowired用法相同
  • @Required:修饰set方法,作用与@Autowired相同,注入对象

  • @Configuration:声明Bean的配置,与@Bean一起使用。
  • @Bean:声明Bean,与xml中使用效果相同,与@Configuration一起使用

  • @importResource:加载包含配置文件信息的xml文件,与@Value一起使用。
  • @Value:获取@importResource处理的配置文件中的配置项,与@importResource一起使用。

  • @PostConstruct:表示被修饰的方法为bean初始化回调方法。
  • @PreDestroy:表示被修饰的方法为bean销毁回调方法。

  • @Scope:标识作用域和代理方式,可以配置@Componment或者@Bean使用

2、XML文件格式及Bean扫描

使用 或者指定Bean的自动检测范围。

能够处理类的注解,功能更强大,并且能够完全覆盖后者。



    
    

在xml文件中可以通过include和exclude进行自定义扫描,节点允许有两个子节点

使用时,一定要在中设置属性use-default-filters=false。


 
 
    

关于type的值,可以参考Spring官网的描述:

3、Bean对象的命名

使用注解可以显示指定Bean对象的名称,如果没有指定,则使用BeanNameGenerator自动生成的名称。

@Component("hello")
public class HelloServiceImpl implements HelloService
{
    public void serviceSave(String arg){
 System.out.println("输入数据是:" + arg);
    }
}

可以自定义命名策略,条件是实现BeanNameGenerator接口(需要包含无参构造函数),并在XML中进行指定。

package service;

import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanNameGenerator;

public class MyNameGenerator implements BeanNameGenerator {
    public String generateBeanName(BeanDefinition definition, BeanDefinitionRegistry registry) {
 return definition.getBeanClassName().toUpperCase();
    }

    public MyNameGenerator() {
    }
}

4、Bean的作用域

使用注解:@Scope("")

@Component
@Scope("prototype")
public class HelloServiceImpl implements HelloService
{
}

可以自定义scope策略,条件是实现ScopemetadataResolver接口(需要包含无参构造函数),并在XML中指定。

public class MyScopemetadataResolver implements ScopemetadataResolver {
    public Scopemetadata resolveScopemetadata(BeanDefinition beanDefinition) {
 Scopemetadata metadata = new Scopemetadata();
 ...
 return metadata;
    }

    public MyScopemetadataResolver() {
    }
}

5、@Required和@Autowired

@Required注解用来修饰set方法,表示受影响的Bean必须在配置时候被设置。

@Autowired注解使用范围更大,可以修饰成员变量,或者set方法或者构造方法,实现的效果是相同的。如果找不到对应的Bean,会注入失败抛出异常,可以通过设置@Autowired中required=false来避免。

@Autowired(required = false)

@Autowired可以修饰集合或者数组。当修饰集合或者数组的时候,Spring会将ApplicationContext中所有对应类型的Bean注入进去。当类中依赖抽象的集合的时候,这种机制可以将所有实现该接口的对象注入到集合中。对于List集合,可以通过在对应Bean上使用@Order注解保证集合中的顺序。

@Repository
@Order(1)
public class DAOImpl implements DAO {
    public void DAOSave(String arg) {
    }
}
@Repository
@Order(2)
public class DAOImplBak implements DAO {
    public void DAOSave(String arg) {
    }
}
@Component
public class HelloServiceImpl implements HelloService
{
    @Autowired
    private List daos;

    public void serviceSave(){

 for (DAO dao : daos)
 {
     System.out.println(dao.getClass().getName());
 }
    }
}
learn.DAO.DAOImpl
learn.DAO.DAOImplBak

Process finished with exit code 0

@Autowired修饰抽象成员的时候,配合使用@Qualifier注解,指定被注入的对象的名称。

    @Autowired
    @Qualifier("DAOImplBak")
    private DAO dao;

6、@Bean

@Bean注解配合@Configuration,能够实现在java代码中,做到与xml配置一样的效果。

@Configuration
public class ServiceConfig {

    @Bean(name = "ttService", initMethod = "init", destroyMethod = "dest")
    public TestService testService()
    {
 return new TestService();
    }
}
public class TestService {

    public void init()
    {
    }

    public void dest()
    {
    }
}

上述代码等同的效果,就是在xml文件中做出如下配置

    

@Bean注解可以用于自定义实现的processer。

使用@Bean注解定义Bean的时候,可以配合使用@Scope,指定Bean的作用域和代理方式。

7、@importResource和@Value注解进行配置文件的加载和使用

@importResource和@Value注解主要用于配置文件的加载和使用。通过@importResource注解指定配置文件的路径,使用@Value注解使用配置文件中的具体配置。

加载配置文件有2中方式,一种是直接通过xml文件进行加载和使用;另外一种是通过注解方式。

通过xml加载和使用:

    
    
 
 
 
 
    

通过注解方式加载和使用:

configuration.xml文件中添加placeholder配置:

在代码中,通过@importResource注解指定placeholder所在的xml文件

@Configuration
@importResource("classpath:configuration.xml")
public class Config {
    @Value("${db.username}")
    private String userName;

    @Value("${db.password}")
    private String password;

    @Value("${db.ip}")
    private String ipAddress;

    @Value("${db.port}")
    private int port;

    @Bean
    public DatabaseConfig databaseConfig()
    {
 return new DatabaseConfig(userName, ipAddress, port, password);
    }
}

8、@Resource注解使用方式与@Autowired使用方式相同

9、@PostConstruct和@PreDestroy

@PostConstruct和@PreDestroy修饰函数,表示bean的初始化回调方法和销毁回调方法。与xml配置方式中指定init-method和destroy-method的效果是相同的。

    @PostConstruct
    public void init()
    {}

    @PreDestroy
    public void destroy()
    {}

10、@Inject注解

@Inject与@Autowired是等效的,用于类,属性,set方法,构造器。

10、@Named注解

@Named与@Component是等效的,用于修饰类。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/235547.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号