标签:java wired red com config util public prope 返回值
@Configuration
注解标记在类上, 就像下面的配置文件. 我们将该类成为配置类.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="userService" class="com.funtl.hello.spring.service.impl.UserServiceImpl" />
</beans>
@Bean
标记在方法上, 方法的返回值相当于向SpringIOC
容器注入一个Bean
. 其中, 返回值相当于xml
文件bean
标签的class
属性, 方法的名称相当于id
属性. 我们的property
属性被放置在了方法之中进行实现. @Bean
注解有一个属性, name
属性 可以帮助我们指定Bean
的id
的名字 .
Spring Boot
实现后端Validator
验证 .
<!-- 配置 Bean Validator 定义 -->
<bean id="validator" class="javax.validation.Validator"/>
<bean id="beanValidator" class="com.funtl.my.shop.commons.validator.BeanValidator">
<property name="validator" ref="validator" />
</bean>
改成配置类
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import pers.ycy.blog.utils.BeanValidator;
import javax.validation.Validator;
@Configuration
public class AutoWired {
private final Validator Validator;
public AutoWired(Validator Validator) {
this.Validator = Validator;
}
@Bean(name="beanValidator")
public BeanValidator getBeanValidator(){
return new BeanValidator(Validator);
}
}
标签:java wired red com config util public prope 返回值
原文地址:https://www.cnblogs.com/A-FM/p/12010261.html