Spring 什么是春天的豆子,什么不是
假设我有这样的代码:Spring 什么是春天的豆子,什么不是,spring,Spring,假设我有这样的代码: @Repository public class Foo{ } @Service public class Boo{ @Autowired private Foo foo; } 那么现在我们叫bean什么呢?Bean是Foo类型的引用“Foo”的对象,但是Boo类是否也被注释为服务,Foo作为存储库?我已经使用spring一段时间了,但这个基本问题让我感到很遗憾,因为我不知道……您的所有应用程序组件(@Component、@Service、@Repository、@C
@Repository
public class Foo{
}
@Service
public class Boo{
@Autowired
private Foo foo;
}
那么现在我们叫bean什么呢?Bean是Foo类型的引用“Foo”的对象,但是Boo类是否也被注释为服务,Foo作为存储库?我已经使用spring一段时间了,但这个基本问题让我感到很遗憾,因为我不知道……您的所有应用程序组件(@Component、@Service、@Repository、@Controller等)都将自动注册为Springbeans
所有应用程序组件(@Component、@Service、@Repository、@Controller等)将自动注册为Springbeans
在Spring的上下文中,bean是Spring管理的对象。这里的SpringManaged是指由SpringIoC容器创建、初始化、管理和销毁的对象 每当我们用
@Component
标记一个类时,Spring IOC容器将为您的类创建对象并管理它,只要我们可以从ApplicationContext
获取它,或者使用@Autowired/@Resource/@Inject
注释访问它
我们还可以使用@Controller、@Repository、@Service、@ControllerAdvice、@Configuration、@Aspect
代替@Component
,更具体地告诉我们的类是服务、存储库或方面等
我们还可以使用@Bean
注释从方法返回值创建Bean
@Configuration
public class SolrConfig {
@Value("${spring.data.solr.host}") String solrUrl;
@Bean
public SolrServer solrServer() {
return new HttpSolrServer(solrUrl);
}
@Bean(name = "solrTemplate")
public SolrTemplate solrTemplate() {
return new SolrTemplate(new HttpSolrServer(solrUrl), RULE_ENGINE_CORE);
}
}
在Spring上下文中,bean是Spring管理的对象。这里的SpringManaged是指由SpringIoC容器创建、初始化、管理和销毁的对象 每当我们用
@Component
标记一个类时,Spring IOC容器将为您的类创建对象并管理它,只要我们可以从ApplicationContext
获取它,或者使用@Autowired/@Resource/@Inject
注释访问它
我们还可以使用@Controller、@Repository、@Service、@ControllerAdvice、@Configuration、@Aspect
代替@Component
,更具体地告诉我们的类是服务、存储库或方面等
我们还可以使用@Bean
注释从方法返回值创建Bean
@Configuration
public class SolrConfig {
@Value("${spring.data.solr.host}") String solrUrl;
@Bean
public SolrServer solrServer() {
return new HttpSolrServer(solrUrl);
}
@Bean(name = "solrTemplate")
public SolrTemplate solrTemplate() {
return new SolrTemplate(new HttpSolrServer(solrUrl), RULE_ENGINE_CORE);
}
}
定义bean可以被认为是替换关键字new
可以找到更多信息,这些信息可能有助于理解Spring中的bean。定义bean可以被认为是替换关键字new 可以找到更多信息,这些信息可能有助于理解Spring中的bean。可能重复的