directory
1. What should be done in the configuration file if annotations are used?
Add one after the beans tag
<context:annotation-config/>
Copy the code
Tag to declare that the annotations will be used.
<?xml version="1.0" encoding="UTF-8"? >
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<! -- Add this tag here -->
<context:annotation-config/>
<bean name="c" class="com.learn.springDemo.Category">
<property name="name" value="Eather"></property>
<property name="id" value="12345"></property>
</bean>
<bean name="p" class="com.learn.springDemo.Product">
<property name="name" value="a product"></property>
<property name="id" value="1008611"></property>
<! Comment out the following line because the code will use autowire to inject cateGroy instances into the Product -->
<! --<property name="category" ref="c"></property>-->
</bean>
</beans>
Copy the code
2. How should annotations be used?
If you’re using @autowired annotations, you can put them in front of two places.
Before properties and before setter methods.
The following code:
package com.learn.springDemo;
import org.springframework.beans.factory.annotation.Autowired;
public class Product {
private int id;
private String name;
// Add it here
@Autowired
private Category category;
public void setId(int id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
// Add it here
@Autowired
public void setCategory(Category category) {
this.category = category;
}
public int getId(a) {
return this.id;
}
public String getName(a) {
return this.name;
}
public Category getCategory(a) {
return this.category; }}Copy the code
Here have to import org. Springframework. Beans. Factory. The annotation. Autowired;
Running result:
a product's id is 1008611 and its category name is Eather
Copy the code
How to inject different instances of the same class?
The last note mentioned the question of whether it is possible to write more than one bean in XML, i.e., what about injecting different instances of the same class? When using XML configuration, it’s simple, just change the name.
This is where the @resource annotation can be used
First, change the XML configuration file
<?xml version="1.0" encoding="UTF-8"? >
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<! -- Add this tag here -->
<context:annotation-config/>
<bean name="c" class="com.learn.springDemo.Category">
<property name="name" value="Eather"></property>
<property name="id" value="12345"></property>
</bean>
<bean name="cc" class="com.learn.springDemo.Category">
<property name="name" value="David"></property>
<property name="id" value="10086"></property>
</bean>
<bean name="p" class="com.learn.springDemo.Product">
<property name="name" value="a product"></property>
<property name="id" value="1008611"></property>
<! -- Comment out the following line -->
<! --<property name="category" ref="c"></property>-->
</bean>
</beans>
Copy the code
Annotations should precede attributes
package com.learn.springDemo;
import org.springframework.beans.factory.annotation.Autowired;
import javax.annotation.Resource;
public class Product {
private int id;
private String name;
@Resource(name = "c")
private Category category;
@Resource(name = "cc")
private Category category1;
public void setId(int id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setCategory(Category category) {
this.category = category;
}
public void setCategory1(Category category1) {
this.category1 = category1;
}
public int getId(a) {
return this.id;
}
public String getName(a) {
return this.name;
}
public Category getCategory(a) {
return this.category;
}
public Category getCategory1(a) {
returncategory1; }}Copy the code
Test code:
package com.learn.springTest;
import com.learn.springDemo.Product;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String args[]) {
ApplicationContext ac = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"});
Product p = (Product) ac.getBean("p");
System.out.println(p.getName() + "'s id is " + p.getId() + " and its category name is " + p.getCategory().getName() + " and "+ p.getCategory1().getName()); }}Copy the code
Running result:
a product's id is 1008611 and its category name is Eather and David
Copy the code
4, XML configuration file can not write beans?
The above examples are annotations to the behavior of the injected object. Can the use of the bean object itself be annotated as well, without being written to the configuration file?
The answer, of course, is yes. Modify the configuration file to remove all beans and bean tags and add
<context:component-scan base-package="com.learn.springDemo"/>
Copy the code
Base-package specifies the package to scan.
<?xml version="1.0" encoding="UTF-8"? >
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<! -- Add this tag here -->
<context:component-scan base-package="com.learn.springDemo" />
</beans>
Copy the code
The class definition code is:
package com.learn.springDemo;
import org.springframework.stereotype.Component;
@Component("c")
public class Category {
private int id;
private String name = "I am a Category";
public int getId(a) {
return this.id;
}
public String getName(a) {
return this.name;
}
public void setId(int id) {
this.id = id;
}
public void setName(String name) {
this.name = name; }}package com.learn.springDemo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component("p")
public class Product {
private int id;
private String name = "I am a product";
@Autowired
private Category category;
public void setId(int id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setCategory(Category category) {
this.category = category;
}
public int getId(a) {
return this.id;
}
public String getName(a) {
return this.name;
}
public Category getCategory(a) {
return this.category; }}Copy the code
The test code is:
package com.learn.springTest;
import com.learn.springDemo.Product;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String args[]) {
ApplicationContext ac = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"});
Product p = (Product) ac.getBean("p");
System.out.println("Product name is" + p.getName() + "What's the category name?"+ p.getCategory().getName()); }}Copy the code
The result of this operation is:
The name product is I'm a product category and the name product is a categoryCopy the code
As for the third question above, I don’t know how to solve it for the time being. Maybe it doesn’t make much sense.