In this section, let's discuss how to inject beans through attributes.
This chapter is divided into two parts, the first part we inject the value through the property to the object, the second part we pass the property to the object to inject another object's reference.
1. How do I inject values into an object through attributes?
(1) Domain
Package Com.raylee.my_new_spring.my_new_spring.ch01.topic_1_7;public class Cake {private final int id = index++;p rivate static int index = 0;private String name = "";p rivate Double size = 0;public string GetName () {return name;} public void SetName (String name) {this.name = name;} Public double GetSize () {return size;} public void SetSize (double size) {this.size = size;} public int getId () {return ID;} @Overridepublic String toString () {return "Create the Cake,its ID:" + ID + ", size:" + size + "inch, Name:" + Name;}}
We only need one cake for this domain class, but we'll add the name and size of the two properties, and then we'll use spring to help us assign values.
(2) Test class:
Package Com.raylee.my_new_spring.my_new_spring.ch01.topic_1_7;import Org.junit.test;import Org.junit.runner.runwith;import Org.springframework.beans.factory.annotation.autowired;import Org.springframework.context.applicationcontext;import org.springframework.test.context.ContextConfiguration; Import Org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith (Springjunit4classrunner.class) @ Contextconfiguration (locations = {"/com/raylee/my_new_spring/my_new_spring/ch01/topic_1_7/ Applicationcontext-test.xml "}) public class Caketest {@Autowiredprivate ApplicationContext applicationcontext;@ testpublic void Testchief () {Cake Cake = Applicationcontext.getbean (Cake.class); System.out.println (Cake.getid ()); System.out.println (Cake.getname ()); System.out.println (Cake.getsize ());}}
Nothing special, just get the bean out and print a few properties.
(3) configuration file
<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" xmlns: context= "Http://www.springframework.org/schema/context" xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:tx= "Http://www.springframework.org/schema/tx" xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" xsi: schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-3.1.xsdhttp://www.springframework.org/schema/context Http://www.springframework.org/schema/context /spring-context-3.1.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/ Spring-tx-3.1.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/ SPRING-UTIL-3.1.XSDHTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/DATA/JPA http://www.springframework.org/schema/data/ Jpa/spring-jpa-1.0.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/spring-aop.xsd "><bean Id= "Cake" class= "com.raylee.my_new_spring.my_new_spring.ch01.topic_1_7.Cake" ><property name= "name" value= " Blueberry Cheesecake "/><property name=" Size "value=" 7 "/></bean></beans>
Configuration file is important, we need to insert the property in the bean tag, and then name this attribute needs to be the same as our Domain class property name.
Note: The first letter here can be case insensitive
That is
<bean id= "Cake" class= "com.raylee.my_new_spring.my_new_spring.ch01.topic_1_7.Cake" ><property name= "name" Value= "Blueberry cheesecake"/><property name= "Size" value= "7"/></bean>
And
<bean id= "Cake" class= "com.raylee.my_new_spring.my_new_spring.ch01.topic_1_7.Cake" ><property name= "name" Value= "Blueberry cheesecake"/><property name= "Size" value= "7"/></bean>
is the same.
But like the full capitalization below, it throws an exception.
<bean id= "Cake" class= "com.raylee.my_new_spring.my_new_spring.ch01.topic_1_7.Cake" ><property name= "name" Value= "Blueberry cheesecake"/><property name= "SIZE" value= "7"/></bean>
Test output:
0
Blueberry Cheesecake
7.0
Summary: This chapter focuses on how to inject values into objects through attributes, as well as the case-sensitive issues that need to be noted in the middle.
Catalog: http://blog.csdn.net/raylee2007/article/details/50611627
My github:https://github.com/raylee2015/my_new_spring.
Get to know Spring-1.7 from scratch how do I inject beans through attributes? (1)-How can I inject values into an object through attributes?