Automatic Assembly
Spring can automatically assemble the dependencies between Bean and Bean without using ref to display specified dependent beans.
Pass The default-autowire attribute of the element can also be specified through the autowire attribute.
No: automatic assembly is not used and must be defined by ref.
ByName: Automatic Assembly Based on Attribute names
ByType: Automatic Assembly Based on the property type
Constructor: automatically assembled Based on the attribute name, using constructor
Autodetect: byType or constructor is determined by beanFactory. There is a default constructor, and byType is used;
Spring will be automatically searched and assembled at startup. If you do not want automatic assembly, you can use
Autowire-candidate = "false"
Example 1:
Reply. java
Package auto; public class Reply {private String id; private String title; private String content; private Topic topic; // constructor public Reply () {System. out. println ("reply created");} public String getId () {return id;} public void setId (String id) {this. id = id;} public String getTitle () {return title;} public void setTitle (String title) {this. title = title;} public String getContent () {return content;} public void setContent (String content) {this. content = content;} public Topic getTopic () {return topic;} public void setTopic (Topic topic) {this. topic = topic ;}}
Topic. java
Package auto; import java. util. List; public class Topic {private String id; private String title; private String Content; // The set inherits the private List
Names; public List
GetNames () {return names;} public void setNames (List
Names) {this. names = names;} public String getId () {return id;} public void setId (String id) {this. id = id;} public String getContent () {return Content;} public void setContent (String content) {Content = content;} public String getTitle () {return title ;} public void setTitle (String title) {this. title = title ;}}
ApplicationContext. xml
1
Xxxx
Yy
1
Zhenhao
Xuexi
Test code
/** Query by XML file */ApplicationContext context = new ClassPathXmlApplicationContext ("applicationContext. xml "); Reply reply = context. getBean ("reply", Reply. class); System. out. println (reply. getTopic (). getTitle ());
Running result:
Reply created
Xxxx
Construct a annotation configuration class through configuration class Query
Config. java
Package cofig; import org. springframework. beans. factory. annotation. value; import org. springframework. context. annotation. bean; import org. springframework. context. annotation. configuration; import auto. topic; // annotation injection, change config to Configuration class @ Configuration public class Config {// assign a Value to the attribute @ value (Value = "csdn") public String name; // obtain the object named topic3 @ Bean (name = "topic3") public Topic getTopic () {Topic topic = new Topic (); topic. setTitle ("java"); topic. setContent ("ccxcc"); return topic ;}}
Test code
/** Construct a annotation configuration class through configuration class query and perform manual intervention */ApplicationContext context = new AnnotationConfigApplicationContext (Config. class); Topic topic = context. getBean ("topic3", Topic. class); System. out. println (topic. getTitle () + "--" + topic. getContent ());
Running result:
Java -- ccxcc
Inherit parent class labels
Configure in XML file
789
Test code
/*** Inherit from the Collection class and output all attributes in the Set */ApplicationContext context = new ClassPathXmlApplicationContext ("applicationContext. xml "); Topic topic = context. getBean ("topic1", Topic. class); System. out. println (topic. getNames ());
Running result:
Reply created
[123,245,789]
Different types of Inheritance
Talk. java
Package auto; import java. util. List; public class Talk {private String id; private String title; private String Content; private List
Names; public String getId () {return id;} // implements the constructor public Talk () {System. out. println ("talk created");} public void setId (String id) {this. id = id;} public String getTitle () {return title;} public void setTitle (String title) {this. title = title;} public String getContent () {return Content;} public void setContent (String content) {Content = content;} public List
GetNames () {return names;} public void setNames (List
Names) {this. names = names ;}}
Configure in XML file
Test code
/*** Different types of inheritance */ApplicationContext context = new ClassPathXmlApplicationContext ("applicationContext. xml "); Talk topic = context. getBean ("talk", Talk. class); System. out. println (topic. getNames (); System. out. println (topic. getTitle ());
Running result:
Talk created
Reply created
[123,245]
Xxxx
Factory TopicFactory. java
Package factory; import org. springframework. beans. factory. FactoryBean; import auto. Topic; public class TopicFactory implements FactoryBean
{Private Topic topic; // returned object @ Overridepublic Topic getObject () throws Exception {// Singleton mode if (topic = null) {topic = new Topic ();} return topic;} // return object type @ Overridepublic Class
GetObjectType () {return Topic. class;} // determines whether the object is a singleton () {return true ;}}
Bean. xml
Test code:
// Factory ApplicationContext context = new ClassPathXmlApplicationContext ("bean. xml "); Topic topic = context. getBean ("topic", Topic. class); topic. setId ("110"); System. out. println (topic. getId (); Topic topic1 = context. getBean ("topic", Topic. class); System. out. println (topic1.getId ());
Running result:
110
110