4. spring di, 4. spring

Source: Internet
Author: User

4. spring di, 4. spring

Spring di, that is, dependency injection. In simple sense, it is to assign values to attributes.

1. Use setter to assign a value to the property tag under the bean in the spring applicationContext. xml configuration file.

Attribute name: Specifies the attribute name and attribute value. It is generally used for packaging of basic data types.

The specified value of attribute ref, which is generally used for reference type and list tag. The value tag below,

The value tag under the set tag, the entry under the map tag, and the key, value,

There is also props, the following prop, key

Public class Student {public void say () {System. out. println ("student") ;}} public class Person {private Long pid; // packing type private String pname; // String type private Student student; // reference type private List lists; private Set sets; public Long getPid () {return pid;} public void setPid (Long pid) {this. pid = pid;} public String getPname () {return pname;} public void setPname (String pname) {this. pname = pname;} public Student getStudent () {return student;} public void setStudent (Student student) {this. student = student;} public List getLists () {return lists;} public void setLists (List lists) {this. lists = lists;} public Set getSets () {return sets;} public void setSets (Set sets) {this. sets = sets;} public Map getMap () {return map;} public void setMap (Map map) {this. map = map;} public Properties getProperties () {return properties;} public void setProperties (Properties properties) {this. properties = properties;} private Map map; private Properties properties ;}

  

<? 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-2.5.xsd"> <bean id = "person" class = "cn. di. xml. set. person "> <! -- Property indicates the basic type (packaging type and String) of the property in spring) you can use value to assign values to reference types. Use ref to assign values. --> <property name = "pid" value = "5"> </property> <property name = "pname" value =" wang Wu "> </property> <property name =" student "> <ref bean =" student "/> </property> <property name =" lists "> <list> <value> list1 </value> <value> list2 </value> <ref bean = "student"/> </list> </property> <property name = "sets"> <set> <value> set1 </value> <value> set2 </value> <ref bean = "student"/> </set> </property> <property name = "map"> <map> <entry key = "map1"> <value> map1 </value> </entry> <entry key = "map2"> <value> map2 </value> </entry> <entry key = "map3"> <ref bean = "student"/> </entry> </map> </property> <property name = "properties"> <props> <prop key = "prop1"> prop1 </prop> </props> </property> </bean> <bean id = "student" class = "cn. di. xml. set. student "> </bean> </beans>

  

@Testpublic void test(){               ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");Person person = (Person)context.getBean("person");person.getStudent().say();System.out.println(person.getPid());System.out.println(person.getPname());List lists = person.getLists();for(int i=0;i<lists.size();i++){System.out.println(lists.get(i).toString());}}

  

2. assign values using Constructors

There is a constructor-arg tag under the bean, which has the index, type, ref, and value attributes.

public class Person {private Long pid;public Long getPid() {return pid;}public String getPname() {return pname;}public Student getStudent() {return student;}private String pname;private Student student;public Person(Long pid,String pname){this.pid = pid;this.pname = pname;}public Person(String pname,Student student){this.pname = pname;this.student = student;}}

  

<? 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-2.5.xsd"> <bean id = "person" class = "cn. di. xml. constructor. person "> <! -- Number of the index parameter of the constructor. The subscript starts from 0. type: ref. If the type is a reference type, assign a value. If the type is a basic type, assign a value: only one constructor can be specified --> <constructor-arg index = "0" type = "java. lang. string "value =" "> </constructor-arg> <constructor-arg index =" 1 "ref =" student "> </constructor-arg> </bean> <bean id = "student" class = "cn. di. xml. constructor. student "> </bean> </beans>

  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.