Beans are ubiquitous in spring and SPRINGMVC, it's important to put this concept into context, and here's what I think:
First, what is a bean?
1, Java object-oriented, the object has methods and properties, then you need an object instance to invoke methods and properties (i.e. instantiation);
2. All classes that have methods or attributes need to be instantiated to use these methods and attributes in order to be figurative;
3. Rule: all subclasses and classes with methods or attributes are added to the annotations of the registered bean to spring IOC ;
4, the bean is understood as the agent or spokesperson of the class (in fact, it is implemented by reflection, proxy), so that it can represent the class has the possession of the thing
5, we are on the Weibo @ over xxx, the other party will first see this message, and give you feedback, then in spring, you identify an @ symbol, then spring will come to see, and get a bean from here or give a bean
Second, the annotations are divided into two categories:
1, one kind is uses the bean, namely is already in the XML file to configure the bean to use, completes the property, the method assembly, for example @autowired, @Resource, May through Bytype (@Autowired), ByName (@Resource) way to get the bean;
2, a class is registered Bean, @Component, @Repository, @ Controller, @Service, @Configration These annotations are the objects you want to instantiate into a bean, placed in the IOC container, when you need to use , it will @autowired with the above, @Resource together, the object, properties, methods to assemble the perfect.
Iii. What is @Bean?
1. What is the principle? First look at the source of some of the content:
1234567891011121314 |
Indicates that a method produces a bean to be managed
by
the Spring container.
<p>The names and semantics of the attributes to
this
annotation are intentionally
similar to those of the {@code <bean/>} element
in
the Spring XML schema. For
example:
<pre
class
=
"code"
>
@Bean
public
MyBean myBean() {
// instantiate and configure MyBean obj
return
obj;
}</pre>
|
It means that @bean clearly indicates a method, what method--a method of generating a bean, and handing it over to the spring container management; From here we understand why @bean is on the annotation of the method, because it tells the annotated method very clearly, you give me a bean, And then hand it over to the spring container, and you leave the rest.
2, remember, @Bean put on the method, is to produce a bean, then you are not confused, because already in your definition of the class added @configration and other registered bean annotations, why also use @bean it? I do not know this, let me give an example, let's discuss it together:
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
package
com.edu.fruit;
//定义一个接口
public
interface Fruit<T>{
//没有方法
}
/*
*定义两个子类
*/
package
com.edu.fruit;
@Configuration
public
class
Apple
implements
Fruit<Integer>{
//将Apple类约束为Integer类型
}
package
com.edu.fruit;
@Configuration
public
class
GinSeng
implements
Fruit<String>{
//将GinSeng 类约束为String类型
}
/*
*业务逻辑类
*/
package
com.edu.service;
@Configuration
public
class
FruitService {
@Autowired
private
Apple apple;
@Autowired
private
GinSeng ginseng;
//定义一个产生Bean的方法
@Bean
(name=
"getApple"
)
public
Fruit<?> getApple(){
System.out.println(apple.getClass().getName().hashCode);
System.out.println(ginseng.getClass().getName().hashCode);
return
new Apple();
}
}
/*
*测试类
*/
@RunWith
(BlockJUnit4ClassRunner.
class
)
public
class
Config {
public
Config(){
super
(
"classpath:spring-fruit.xml"
);
}
@Test
public
void
test(){
super
.getBean(
"getApple"
);
//这个Bean从哪来,从上面的@Bean下面的方法中来,返回
的是一个Apple类实例对象
}
}
|
The above example also confirms what I have summed up above:
1. All subclasses and classes with attributes and methods are registered beans to spring and administered by them;
2, @Bean used in the method, tell the spring container, you can get a Bean from the following method
The understanding of bean and @bean in spring