Spring 常用注解篇

这篇具有很好参考价值的文章主要介绍了Spring 常用注解篇。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

Spring 注解篇



一、注解

1.用于创建对象的注解

定义:是指由一个工厂对象决定创建出哪一种产品类

提示:这些注解的作用和在xml中配置标签的作用一样。

● @Component
作用: 用于将当前类对象存入Spring容器中
属性:value属性用于指定存入spring容器中的bean的id,不配置时默认为:当前类的类名首字母转小写

● @Controller
一般用在表现层。
作用、属性同@Component完全一样。

● @Service
一般用在业务层。
作用、属性同@Component完全一样。

● @Repository
一般用在持久层。
作用、属性同@Component完全一样。

提示:@Controller、@Service、@Repository三个注解的作用、属性同@Component完全一样

2.用于注入数据的注解

这些注解的作用和xml配置的标签下的标签的作用是一样的。
@Autowired、@Qualifier、@Resource三个注解只能注入bean类型的数据,不能注入基本数据类型和String类型。

● @Autowired
作用:
自动按照类型注入,只要容器中有唯一的一个bean对象类型和要注入的变量类型匹配,就可以注入成功。
如果IoC容器中没有任何bean的类型和要注入的变量的类型匹配,则报错。
如果IoC容器中该类型的bean存在多个,则将要注入的变量的变量名作为bean的id进行二次匹配:
如果根据变量名可以找到唯一的bean,则进行注入。
如果根据变量名匹配不到,则报错。
出现位置:可以使变量上,也可以是方法上。
细节:在使用注解进行注入时,变量的setter方法就不是必须的了。

@Autowired有一个属性required,默认为true。如果设置为false,则该对象可以在获取不到bean时默认为null。

  1. 标注在属性上
public class Boss {
    @Autowired
    private Car car;
}
  1. 标注在方法上
    Spring容器创建当前对象,就会调用方法,完成赋值。
    方法使用的参数,自定义类型的值从ioc容器中获取。
    示例:
@Autowired
public void setCar(Car car) {
    this.car = car;
}
  1. 标注在有参构造器上
    默认加在ioc容器中的组件,容器启动会调用无参构造器创建对象,再进行初始化赋值等操作。
    给bean移除无参构造器,添加一个有参构造器,为该有参构造器添加@Autowired注解。Spring容器启动时会调用该有参构造器,并从ioc容器中获取参数对应类型的bean对象进行注入。
@Autowired
public Boss(Car car) {
    this.car = car;
}
  1. 标注在构造器参数位置
public Boss(@Autowired Car car) {
    this.car = car;
}

如果当前类只有一个有参构造器,Spring容器创建该bean只能调用该有参构造器,有参构造器的@Autowired可以省略,参数依然可以从ioc容器中获取:
使用构造函数进行注入时,需要标注final来表示这个注入的变量不能被改变。

在@Configuration类中通过@Bean将Color加入容器,并使用@Autowired注入Car:

● @Qualifier
作用:
在按照类型匹配的基础上,再按照名称匹配注入。
它在给类的成员变量注入时,不能单独使用,要和@Autowired配合使用。当按照类型注入冲突时配合@AutoWired
它在给方法参数进行注入时,可以单独使用。
属性:
value:用于指定要注入的bean的id。

● @Resource
作用:直接按照bean的id进行注入。它可以独立使用。
属性:
name:用于指定bean的id。

● @Value
作用:用于注入基本类型和String类型的变量
属性:
value:用于指定数据的值。可以配置:字面量、${key}(从环境变量、配置文件中获取值)、使用Spring中的SpEL(Spring中的EL表达式:#{表达式})。

public class Person {

    @Value("张三")
    private String name;

    @Value("#{ 10 + 8} ")
    private Integer age;

    @Value("${person.nickName}")
    private String nickName;

    // getter/setter方法
}

●@Inject

和@Autowired功能类似,但是没有required属性。
需要导入包

<dependency>
    <gourpId>javax.inject</groupId>
    <artifactId>javax.inject</artifactId>
    <version>1</version>
</dependency>

3.用于改变作用范围的注解

提示:这些注解的作用和xml配置的标签的scope属性的作用一样。

●@Scope
作用:用于指定bean的作用范围。
属性:
value:指定范围的取值。常用取值:singleton、prototype。(不配置时默认为singleton)

● value
取值:
● ConfigurableBeanFactory.SCOPE_SINGLETON : “singleton”,单实例(默认值),ioc容器启动就会调用方法创建对象到ioc容器中。
● ConfigurableBeanFactory.SCOPE_PROTOTYPE : “prototype”,多实例,ioc容器启动时并不会调用方法创建对象,每次获取时才会调用方法创建对象。
● WebApplicationContext.SCOPE_REQUEST:“request”,同一个请求创建一个实例
● WebApplicationContext.SCOPE_SESSION:“session”,同一个session创建一个实例

● scopeName
同’value’

●@Profile
Spring为我们提供的可以根据当前环境,动态的激活和切换一系列Bean的功能。
指定组件在哪个环境的情况下才能被注册到容器中,不指定时在任何环境下都能注册这个组件。
加了@Profile环境标识的bean,只有这个环境被激活的时候才能被注册到容器中。默认是default环境。
例如:

@Profile("default")
@Bean("defaultDataSource")
public DataSource dataSourceDefault(@Value("${db.password}") String pwd) throws Exception{
    ComboPooledDataSource dataSource = new ComboPooledDataSource();
    dataSource.setUser(user);
    dataSource.setPassword(pwd);
    dataSource.setJdbcUrl("jdbc:oracle:thin:@localhost:1521:orcl");
    dataSource.setDriverClass(driverClass);
    return dataSource;
}

@Profile("test")  // value可以随便自定义
@Bean("color")
public Color color() {
    return new Color();
}

@Profile("test")
@Bean("testDataSource")
public DataSource dataSourceTest(@Value("${db.password}") String pwd) throws Exception{
    ComboPooledDataSource dataSource = new ComboPooledDataSource();
    dataSource.setUser(user);
    dataSource.setPassword(pwd);
    dataSource.setJdbcUrl("jdbc:oracle:thin:@localhost:1521:orcl");
    dataSource.setDriverClass(driverClass);
    return dataSource;
}

@Profile("dev")
@Bean("devDataSource")
public DataSource dataSourceDev(@Value("${db.password}")String pwd) throws Exception{
    ComboPooledDataSource dataSource = new ComboPooledDataSource();
    dataSource.setUser(user);
    dataSource.setPassword(pwd);
    dataSource.setJdbcUrl("jdbc:oracle:thin:@localhost:1521:orcl");
    dataSource.setDriverClass(driverClass);
    return dataSource;
}

切换激活profile环境:(不切换时,系统默认为default环境)
使用 JVM 命令行参数

添加 VM arguments:-Dspring.profiles.active=test,便可以将环境切换到test环境

使用ioc无参构造器,激活环境配置

如果使用有参构造器AnnotationConfigApplicationContext(Class<?>… annotatedClasses)获取ioc容器,该构造函数中会执行以下代码:

可以使用无参构造器手动调用这些方法,实现这个效果。同时,可以在注册配置类之前,切换环境配置,防止把配置类中所有配置全部加载:

通过无参构造器获取ioc容器
通过ioc容器的environment对象切换环境(可以同时配置多个环境)
调用register注册主配置类
调用refresh加载bean

public AnnotationConfigApplicationContext(Class<?>... annotatedClasses) {
    this();
    register(annotatedClasses);
    refresh();
}
@Test
public void test01() {
   AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
   applicationContext.getEnvironment().setActiveProfiles("dev", "test");
   applicationContext.register(MainConfigOfProfile.class);
   applicationContext.refresh();
   
   String[] beans = applicationContext.getBeanNamesForType(DataSource.class);
   Arrays.asList(beans).forEach(System.out::println);
}

@Profile也可以注解在配置类上:只有在指定的环境的时候,整个配置类里面的所有配置才能生效。

如果此时激活的环境为dev:虽然devDataSource这个bean上配置的环境是dev,但是因为整个配置类的环境是test,所以整个配置类都不进行加载,devDataSource也不会进行加载。

@Profile("test")
@PropertySource("classpath:/dbconfig.properties")
@Configuration
public class MainConfigOfProfile implements EmbeddedValueResolverAware {

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

    private StringValueResolver resolver;
    private String driverClass;

    @Profile("default")
    @Bean("defaultDataSource")
    public DataSource dataSourceDefault(@Value("${db.password}") String pwd) throws Exception{
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        dataSource.setUser(user);
        dataSource.setPassword(pwd);
        dataSource.setJdbcUrl("jdbc:oracle:thin:@localhost:1521:orcl");
        dataSource.setDriverClass(driverClass);
        return dataSource;
    }

    @Profile("test")
    @Bean("testDataSource")
    public DataSource dataSourceTest(@Value("${db.password}") String pwd) throws Exception{
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        dataSource.setUser(user);
        dataSource.setPassword(pwd);
        dataSource.setJdbcUrl("jdbc:oracle:thin:@localhost:1521:orcl");
        dataSource.setDriverClass(driverClass);
        return dataSource;
    }

    @Profile("dev")
    @Bean("devDataSource")
    public DataSource dataSourceDev(@Value("${db.password}")String pwd) throws Exception{
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        dataSource.setUser(user);
        dataSource.setPassword(pwd);
        dataSource.setJdbcUrl("jdbc:oracle:thin:@localhost:1521:orcl");
        dataSource.setDriverClass(driverClass);
        return dataSource;
    }

    @Profile("prod")
    @Bean("prodDataSource")
    public DataSource dataSourceProd(@Value("${db.password}")String pwd) throws Exception{
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        dataSource.setUser(user);
        dataSource.setPassword(pwd);
        dataSource.setJdbcUrl("jdbc:oracle:thin:@localhost:1521:orcl");
        dataSource.setDriverClass(driverClass);
        return dataSource;
    }

    @Override
    public void setEmbeddedValueResolver(StringValueResolver resolver) {
        this.resolver = resolver;
        this.driverClass = resolver.resolveStringValue("${db.driverClass}");
    }
}

4.和生命周期相关的注解

提示:这些注解的作用和xml配置的标签的init-method、destroy-method属性作用相同。

● @PreDestroy
作用:用于指定销毁方法。

● @PostConstruct
作用:用于指定初始化方法。

@Service("accountService")
@Scope("prototype")
public class AccountServiceImpl implements IAccountService {

    @Value("${server.port}")
    private String port;
    
    @Resource(name = "accountDao")
    private IAccountDao accountDao;
    
    @Autowired
	  @Qualifier("hAccountDao")
    private HAccountDao hAccountDao;

    public void saveAccount() {
        accountDao.saveAccount();
    }

    @PostConstruct
    public void init() {
        System.out.println("初始化方法");
    }

    @PreDestroy
    public void destroy() {
        System.out.println("销毁方法");
    }
}

● @Lazy
作用:针对单实例的Bean。单实例Bean默认在容器启动时创建对象。懒加载在第一次使用Bean时才创建对象并初始化。

@Bean
@Lazy
public Person person() {
    System.out.println("方法调用了。");
    return new Person("ZhangSan", 10);
}

● @Conditional
作用:照条件进行判断,满足条件才给容器中注册Bean。该注解在Spring、SpringBoot底层中经常使用

value属性需要传入一个判断条件,该判断条件是一个实现了Condition接口的类。

自定义判断条件类

public class MyCondition implements Condition {
    @Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
        // 获取类加载器
        ClassLoader classLoader = context.getClassLoader();
        // 获取ioc使用的beanFactory
        ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
        // 获取到bean定义的注册类
        BeanDefinitionRegistry registry = context.getRegistry();
        ResourceLoader resourceLoader = context.getResourceLoader();

        // 获取当前环境信息
        Environment environment = context.getEnvironment();
        String osName = environment.getProperty("os.name");
        return osName.startsWith("Linux");
    }
}

测试小技巧:
os.name是获取当前系统,默认取的当前系统的系统名称,但是我们可以在 JVM 参数上强制给一个值。
VM options: -Dos.name=linux
在bean上加上判断条件

@Bean("person01")
@Conditional(MyCondition.class)
public Person person01() {
    return new Person("lisi", 18);
}

该注解也可以用在类上,对类中所有bean统一设置:

@Configuration
@Conditional(MyCondition.class)
public class MyConfig {

    @Bean("person")
    public Person person() {
        return new Person("ZhangSan", 10);
    }

    @Bean("person01")    
    public Person person01() {
        return new Person("lisi", 18);
    }
}

5.在配置类中利用java方法创建bean

提示:当我们需要在程序中使用commons-dbutils去操作数据库时,需要创建DataSource、QueryRunner对象存入Spring的容器。创建对象、存入容器的过程可以放在一个配置类中,将创建对象的方法返回值作为bean存入容器。

设置配置类的不同方式:

  1. 在AnnotationConfigApplicationContext构造方法中传参:
  2. 使用@Configuration注解声明配置类:(需要在主配置类中使用@ComponentScan注解扫描到该配置类所在的包)
  3. 使用@Import注解导入其他配置

● @Configuration
作用:指定当前类为一个配置类
细节:当该类作为AnnotationConfigApplicationContext对象创建的参数时,该注解可以不写。

● @ComponentScan
作用:指定Spring在创建容器时要扫描的包。作用和xml中配置的context:component-scan一样。
属性:value/basePackages:两个属性的作用一样,都是指定创建容器时要扫描的包。属性的值为数组。

● value
要扫描的包,等同于basePackages
● basePackages
同value
● includeFilters
指定只包含的组件
属性值是一个@Filter数组
● excludeFilters
指定排除的组件。
属性值是一个@Filter数组

示例:扫描com.study包下的组件,排除@Controller、@Service注解的类

@Configuration
@ComponentScan(value = "com.study", excludeFilters = {
    @ComponentScan.Filter(type = FilterType.ANNOTATION, classes = {Controller.class, Service.class})
})
public class MyConfig {

    @Bean
    public Person person() {
        return new Person("ZhangSan", 10);
    }
}

示例:只包含@Controller、@Service注解的类。(需要先禁用默认的过滤规则useDefaultFilters=false,因为默认的过滤规则是全部扫描)

@Configuration
@ComponentScan(value = "com.study",
        useDefaultFilters = false,
        includeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION, classes = {Controller.class, Service.class})
})
public class MyConfig {

    @Bean
    public Person person() {
        return new Person("ZhangSan", 10);
    }
}

● @ComponentScans
作用:指定Spring在创建容器时要扫描的包。作用和xml中配置的context:component-scan一样。
属性:value/basePackages:两个属性的作用一样,都是指定创建容器时要扫描的包。属性的值为数组。

@ComponentScan使用了 JDK8的@Repeatable的注解,表示这个注解可以在一个类上多次使用。
如果使用的 JDK7,则可以使用@ComponentScans,value属性为 @ComponentScan数组。
● @Bean
作用:将方法的返回值作为一个bean对象,存入Spring的IoC容器中。
属性:value/name:两个属性的作用一样,用于指定bean的id。不配置该属性时,默认以方法名作为id。
细节:当我们给方法配置@Bean注解时,如果方法有参数,Spring会去容器中查找有无可用的bean对象。查找的方式和@Autowired相同。

●@ComponentScan.Filter
作用:配置包扫描的过滤规则

@Filter
type
要排除的方式:FilterType。
Annotation:按照注解
Assignable_type:按照给定的类
AspectJ:使用AspectJ表达式
Regex:使用正则表达式
Custom:使用自定义规则,该规则必须是TypeFilter的实现类
value
要排除的类型,同classes
classes
同value
pattern
正则表达式
自定义TypeFilter规则:
MetadataReader:读取到的当前正在扫描的类的信息
MetadataReaderFactory:可以获取到其他任何类的信息

实例:

public class MyTypeFilter implements TypeFilter {

    @Override
    public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException {
        // 获取当前正在扫描类的注解信息
        AnnotationMetadata annotationMetadata = metadataReader.getAnnotationMetadata();
        // 获取当前正在扫描的类的类信息
        ClassMetadata classMetadata = metadataReader.getClassMetadata();
        // 获取当前的资源信息
        metadataReader.getResource();

        String className = classMetadata.getClassName();

        return "com.study.book.dao.BookDao".equals(className);
    }
}

配置:

@Configuration
@ComponentScan(value = "com.study",
        useDefaultFilters = false,
        includeFilters = {
                @ComponentScan.Filter(type = FilterType.CUSTOM, classes = {MyTypeFilter.class})
        })
public class MyConfig {
    // ...
}

● @Import
作用:用于导入其他的配置类。
属性:value:用于指定要导入的其他配置类的字节码文件。当使用@Import的注解之后,有@Import注解的就是主配置类,而导入的都是子配置类。

使用方式:

@Configuration
public class JdbcConfiguration {
  // ....
}
@Configuration
@ComponentScan("com.study")
@Import(JdbcConfiguration .class)
public class SpringConfiguration {

    @Bean("runner")
    @Scope("prototype")  // Spring容器的bean默认为单例,为避免不同数据库操作之间的干扰,此处应该使用Scope将runner指定为多例
    public QueryRunner createQueryRunner(DataSource dataSource) {
        return new QueryRunner(dataSource);
    }

    @Bean("dataSource")
    public DataSource createDataSource() {
        try {
            ComboPooledDataSource dataSource = new ComboPooledDataSource();
            dataSource.setDriverClass("oracle.jdbc.driver.OracleDriver");
            dataSource.setJdbcUrl("jdbc:oracle:thin:@127.0.0.1:1521/orcl");
            dataSource.setUser("springtest");
            dataSource.setPassword("tiger");
            return dataSource;
        } catch (PropertyVetoException e) {
            throw new RuntimeException(e);
        }
    }
}
	@Test
    public void testFindAll(){
        // 读取配置类中的配置(如果有多个配置类,也可以传入多个配置类)
        ApplicationContext ac = new AnnotationConfigApplicationContext(SpringConfiguration.class);
        IAccountService as = ac.getBean("accountService", IAccountService.class);
        as.findAllAccount().forEach(System.out::println);
    }

给容器中注册组件:

● 包扫描+组件标注注解(@Controller、@Service、@Repository、@Component)[自己写的类]
● @Bean [导入的第三方包里面的组件]
● @Import[快速的给容器中导入一个组件]
● 使用Spring提供的FactoryBean(工厂bean)

自定义ImportSelector:

AnnotationMetadata:当前标注@Import注解的类的所有注解信息。
返回值不能是null,否则会报空指针异常。如果需要返回的内容,则需使用:return new String[0]。

public class MyImportSelector implements ImportSelector {
    @Override
    public String[] selectImports(AnnotationMetadata importingClassMetadata) {
        
        return new String[]{"com.study.bean.Yellow", "com.study.bean.Blue"};
    }
}

自定义ImportBeanDefinitionRegistrar:

AnnotationMetadata:当前类的注解信息
BeanDefinitionRegistry:BeanDefinition注册类。需要添加到容器中的Bean,可以通过BeanDefinitionRegistry.registerBeanDefinition手工注册进来。

public class MyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {
    @Override
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
        String[] beanDefinitionNames = registry.getBeanDefinitionNames();
        Arrays.asList(beanDefinitionNames).forEach(System.out::println);

        // 判断ioc容器中是否注册了指定id的Bean
        boolean redDefinition = registry.containsBeanDefinition("com.study.bean.Red");
        boolean blueDefinition = registry.containsBeanDefinition("com.study.bean.Blue");
   
        if(redDefinition && blueDefinition) {
            // 调用registry的registerBeanDefinition方法手工注册Bean
            // 使用RootBeanDefinition指定要注册的Bean的类型
            RootBeanDefinition beanDefinition = new RootBeanDefinition(RainBow.class);
            // 指定注册进容器的bean的id
            registry.registerBeanDefinition("rainBow", beanDefinition);
        }
    }
}

配置示例:

@Configuration
@Conditional(MyCondition.class)
@Import({Color.class, Red.class, MyImportSelector.class})  // 可以同时配置要导入的类名、ImportSelector、ImportBeanDefinitionRegistrar
public class MyConfig {

    @Bean("person")
    public Person person() {
        return new Person("ZhangSan", 10);
    }
}

推荐博文:https://blog.csdn.net/qq_15719169/article/details/119252781

6.从properties配置文件中获取配置

提示: 使用@PropertySource指定读取的配置文件路径:

@PropertySource
是一个可重复使用的注解。

●@PropertySource的value属性可以传多个路径。

或者使用@PropertySources指定多个@PropertySource

编写jdbcConfig.properties配置文件:

jdbc.driver=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc:oracle:thin:@127.0.0.1:1521/orcl
jdbc.username=springtest
jdbc.password=tiger

在JdbcConfig.java中获取properties中配置的信息

@Configuration
@ComponentScan({"com.study","com.config"})
@PropertySource("classpath:jdbcConfig.properties")
public class SpringConfiguration {

}

在JdbcConfig.java中获取properties文件中的配置信息

@Configuration
public class JdbcConfig {

    @Value("${jdbc.driver}")
    private String jdbcDriver;
    @Value("${jdbc.url}")
    private String jdbcUrl;
    @Value("${jdbc.username}")
    private String jdbcUser;
    @Value("${jdbc.password}")
    private String jdbcPassword;
    
    @Bean("dataSource")
    public DataSource createDataSource() {
        try {
            ComboPooledDataSource dataSource = new ComboPooledDataSource();
            dataSource.setDriverClass(jdbcDriver);
            dataSource.setJdbcUrl(jdbcUrl);
            dataSource.setUser(jdbcUser);
            dataSource.setPassword(jdbcPassword);
            return dataSource;
        } catch (PropertyVetoException e) {
            throw new RuntimeException(e);
        }
    }
}

二、接口

1.FactoryBean接口

Spring提供的 工厂Bean接口

自定义FactoryBean,用来生成Color的Bean:

public class ColorFactoryBean implements FactoryBean<Color> {
    // 返回一个color对象,这个对象会添加进容器中
    @Override
    public Color getObject() throws Exception {
        return new Color();
    }

    // 返回的对象的类型
    @Override
    public Class<?> getObjectType() {
        return Color.class;
    }

    // 是否为单例:true代表这个bean是单实例的,false代表每次获取都会创建一个新的bean
    @Override
    public boolean isSingleton() {
        return true;
    }
}

添加该FactoryBean为Bean:

public class ColorFactoryBean implements FactoryBean<Color> {
    // 返回一个color对象,这个对象会添加进容器中
    @Override
    public Color getObject() throws Exception {
        return new Color();
    }
    // 返回的对象的类型
    @Override
    public Class<?> getObjectType() {
        return Color.class;
    }
    // 是否为单例:true代表这个bean是单实例的,false代表每次获取都会创建一个新的bean
    @Override
    public boolean isSingleton() {
        return true;
    }
}

在获取colorTest这个Bean时,实际获取到的bean对象的类型是ColorFactoryBean::getObjectType()返回的类型,即Color类型。获取到的Bean对象是ColorFactoryBean::getObject()创建的对象。

如果想要获取ColorFactoryBean类型的bean对象,需要在BeanID前添加前缀&,即&colorTest。这个前缀是在BeanFactory.FACTORY_BEAN_PREFIX中定义的。

2.Aware接口

自定义组件想要使用Spring容器底层的一些组件(例如ApplicationContext、BeanFactory、BeanName等),实现xxxAware接口,在创建对象的时候,会调用接口规定的方法,注入相应的组件。

xxxAware的总接口为Aware接口。xxxAware的对应的解析为xxxAwarePropressor。

示例:

在Bean中注入ioc容器ApplicationContext,实现ApplicationContextAware接口


public class Car implements ApplicationContextAware {

    private ApplicationContext applicationContext;

    public Car() {
        System.out.println("Car constructor...");
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        System.out.println("为car注入applicationContext");
        this.applicationContext = applicationContext;
    }
}

String值解析器(可以解析String、SpEL表达式、环境变量值、配置文件值):文章来源地址https://www.toymoban.com/news/detail-477019.html


public class Person implements  EmbeddedValueResolverAware {
    @Override
    public void setEmbeddedValueResolver(StringValueResolver resolver) {
        String str = resolver.resolveStringValue("test: #{ 10 * 2} ==== ${person.nickName} ==== ${os.name} .");
        System.out.println(str);
    }
}

到了这里,关于Spring 常用注解篇的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Spring使用注解进行对象装配(DI)

    通过五大类注解可以更便捷的将对象存储到 Spring 中,同样也可以使用注解将已经储存的对象取出来,直接赋值到注解所在类的一个属性中,这一个过程也叫做对象的装配或者叫对象的注入,即 DI。 获取 Bean 对象也叫做对象装配,就是把对象取出来放到某个类中,有时候也叫

    2024年02月15日
    浏览(31)
  • 【JavaEE进阶】 Spring使用注解存储对象

    在博主前面所写的《【JavaEE进阶】 Spring 的创建和使⽤》中我们已经可以实现基本的 Spring 读取和存储对象的操作了,但在操作的过程中我们发现读取和存储对象并没有想象中的那么“简单”,所以接下来我们要学习更加简单的操作 Bean 对象的⽅法。 在 Spring 中想要更简单的存

    2024年02月05日
    浏览(36)
  • 3. Spring 更简单的读取和存储对象(五大类注解 & 方法注解)

    目录 1. 存储 Bean 对象 1.1 配置扫描路径 1.2 添加注解存储 Bean 对象 1.2.1 @Controller(控制器存储) 1.2.2 @Service(服务存储) 1.2.3 @Repository(仓库存储) 1.2.4 @Component(组件存储) 1.2.5 @Configuration(配置存储) 1.3 使用多个类注解的原因 1.3.1 五大注解之间的关系 1.3.1 Bean 的命名

    2024年02月15日
    浏览(24)
  • 【Spring】(三)Spring 使用注解存储和读取 Bean对象

    前面的文章详细的介绍了 Spring 对象的创建,以及对 Bean 对象的存取操作,但是通过配置文件注册 Bean 对象以及使用 ApplicationContext 或 BeanFactory 的方式获取 Bean 对象的操作就显得格外的复杂。因此,本文主要就是详细介绍了一种更加简单的方式来实现对 Bean 对象更加简单的储

    2024年02月14日
    浏览(28)
  • 【Spring】使用注解读取和存储Bean对象

     哈喽,哈喽,大家好~ 我是你们的老朋友: 保护小周ღ    谈起Java 圈子里的框架,最年长最耀眼的莫过于 Spring 框架啦,本期给大家带来的是:  将对象存储到 Spring 中、Bean 对象的命名规则、从Spring 中获取bean 对象、注入的、@Autowired 与 @Resource 的区别、解决@Bean 方

    2024年02月13日
    浏览(38)
  • SPRING常用注解及其作用

    SPRING常用注解及其作用 1)声明bean的注解 @Component是一种注解,用于标识一个类作为组件(Component)。组件是Spring中的一个通用术语,用于表示可被Spring容器管理和使用的对象。通过该注解,可以实现组件的自动扫描、实例化、依赖注入和配置管理等功能。 @Service是Spring框架

    2024年02月08日
    浏览(77)
  • Spring&SpringBoot常用注解

    在Spring和SpringBoot中,注解是一种非常重要的编程方式,它可以简化代码,提高开发效率。 @SpringBootApplication是SpringBoot应用程序的核心注解,通常用于主类上。它包含了以下三个注解: @Configuration:表示该类是一个配置类,用于定义Spring的配置信息。允许在 Spring 上下文中注册

    2024年02月13日
    浏览(32)
  • Spring MVC常用注解

    @PathVariable、@RequestBody和@RequestParam @PathVariable、@RequestBody和@RequestParam是Spring MVC框架中用于处理请求参数的注解。 @PathVariable:用于处URL路径中的 占位符参数 。例如,对于URL /users/{id} ,其中的 {id} 是一个占位符,可以使用@PathVariable注解将其绑定到方法的参数上,如下所示:

    2024年02月09日
    浏览(29)
  • Spring 常用注解篇

    Spring 注解篇 定义:是指由一个工厂对象决定创建出哪一种产品类 提示:这些注解的作用和在xml中配置标签的作用一样。 ● @Component 作用: 用于将当前类对象存入Spring容器中 属性:value属性用于指定存入spring容器中的bean的id,不配置时默认为:当前类的类名首字母转小写 ●

    2024年02月08日
    浏览(78)
  • spring中常用注解

    1.@EnableCustomConfig @EnableCustomConfig:这是一个自定义的注解,用于启用自定义的配置。它可能是你的应用程序中定义的一个自定义注解,用于加载特定的配置。 2.@EnableCustomSwagger2 @EnableCustomSwagger2:这是一个自定义的注解,用于启用自定义的Swagger2配置。Swagger2是一个用于构建、文

    2024年02月13日
    浏览(22)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包