Spring learning a (dependency injection / Bean / notes, etc.)

  • 1.Spring dependency injection mode.
  • 2. Depending on the type of injection
  • 3.Bean scope
  • 4. The automatic injection
  • 5. manner annotations
  • 6. The spring incorporated in the file attribute profile

1.Spring dependent manner injection

  The usual java developers, programmers need to rely on in a class in other class methods, it usually is dependent on a new instance of the class and then call the class methods, this problem is the development of new poor class instance unified management.

  spring proposed idea of ​​dependency injection, i.e., not by the programmer instantiates the class dependent, but we specify new instance and the instance is injected into the object's class needs to help by spring containers. Another argument dependency injection is the "inversion of control."

  Popular understanding is: We usually new one instance, control of this example is that we programmers, and inversion of control refers to the new instance of work to do but could not help us programmers to spring container to do.

  Spring IOC achieved by DI (dependency injection) (Inversion of Control), commonly used injection method there are three: Constructor injection, set the method parameter injection, interface injection, where we first use Constructor injection, set the method parameter injection .

1.1 Dependency injection is completed by the method set

 Hello built entity classes and method for generating a set

 

private String name;
private Integer age;

 

In the configuration file written into the code to perform the method set dependency injection

 

<bean id="he" class="com.zhiyou100.spring.Hello">
    <property name="name" value="地方大师傅"/>
    <property name="age" value="33"/>
</bean>

Written test class for testing

1 public class Test {
2     public static void main(String[] args) {
3         ApplicationContext app=new ClassPathXmlApplicationContext("app.xml");
4         Hello hh = (Hello) app.getBean("he");
5         System.out.println(hh);
6     } 
7 }

The results output

 

 

 Generally, we recommend using the set method dependency injection

Injected via the constructor 1.2

Generating method in the above three configurations entity class, without parameters, and full participation unary

// no parameters 
public the Hello () {
     Super ();
}
// unary 
public the Hello (String name) {
     Super ();
     the this .name = name;
}
//全参
public Hello(String name, Integer age) {
    super();
    this.name = name;
    this.age = age;
}

Dependency injection profile, passing in two parameters to find the two parameters above to find a single reference to the

<bean id="he2" class="com.zhiyou100.spring.Hello">
    <constructor-arg index="0" value="李四"/>
    <constructor-arg index="1" value="13"/>
</bean>

carry out testing

1 public class Test {
2     public static void main(String[] args) {
3         ApplicationContext app=new ClassPathXmlApplicationContext("app.xml");
4         Hello hh = (Hello) app.getBean("he2");
5         System.out.println(hh);
6     } 
7 }

Test results, the success of dependency injection

 

2. Depending on the type of injection

Strings using basic data types and value attribute if the object type is introduced using the ref attribute

Add a new attribute type based on the entity based on the above

private Student student;
private List<String> list;
private Map<Integer, String> map;

Entity corresponding student class

 1 public class Student {
 2     private String addr;
 3     public String getAddr() {
 4         return addr;
 5     }
 6     public void setAddr(String addr) {
 7         this.addr = addr;
 8     }
 9     @Override
10     public String toString() {
11         return "Student [addr=" + addr + "]";
12     }
13 }

Write configuration code

 1 <bean id="he" class="com.zhiyou100.spring.Hello">
 2         <property name="name" value="地方大师傅"/>
 3         <property name="age" value="33"/>
 4         <property name="student" ref="stu"/>
 5         <property name="list">
 6             <list>
 7                 <value</Jon Snow>value> 
. 8                  < value > Stark three Sa </ value > 
. 9                  < value > Ti Liang </ value > 
10              </ List > 
. 11          </ Property > 
12 is          < Property name = "Map" > 
13 is              < Map > 
14                  < entry Key = ". 1" value = "ah ah" /> 
15                  < entry Key = "2" value = "fact"/>
16                 <entry key="3" value="顶顶顶"/>
17             </map>
18         </property>
19     </bean>
20     
21     <bean id="stu" class="com.zhiyou100.spring.Student">
22         <property name="addr" value="温哥华"/>
23     </bean> 

Write test code, test:

1 public class Test {
2     public static void main(String[] args) {
3         ApplicationContext app=new ClassPathXmlApplicationContext("app.xml");
4         Hello hh = (Hello) app.getBean("he");
5         System.out.println(hh);
6         System.out.println(hh.getMap());
7     } 
8 }

After successful test shown below

3.Bean scope of
  Bean scopes default singleton

  scope: represents the scope of the bean, the default singleton, struts framework requires non-singleton

  prototype: native, non-singleton

<bean id="he" class="com.zhiyou100.spring.Hello" scope="prototype"/>

4. The automatic injection

And two new entity classes UserDao UserService

public class UserDao {
    private String uname;
    public String getUname() {
        return uname;
    }
    public void setUname(String uname) {
        this.uname = uname;
}
public class UserService {
    private UserDao userDao;
    public UserDao getUserDao() {
        return userDao;
    }
    public void setUserDao(UserDao userDao) {
        System.out.println("setUserDao");
        this.userDao = userDao;
    }
}

xml configuration file code, wherein the dependency using an automatic injection userservice autowire = "byName" other explanation for this property

byType: Depending on the type userDao properties, find matching bean

private UserDao userDao;

byName: find matching according to the bean id attribute name

no: the need to manually inject

default: take global default- autowire not set

<bean id="userDao" class="com.zhiyou100.spring.UserDao"/>
    
<bean id="userservice" class="com.zhiyou100.spring.UserService" autowire="byName"/>

Writing tests

public class UserTest {
    public static void main(String[] args) {
        ApplicationContext app=new ClassPathXmlApplicationContext("appUser.xml");
    } 
}

Test results

5. manner annotations

1. The introduction of a jar jar package aop
2. Pack scan profile used.  
<context:component-scan base-package="com.zhiyou100.zhl"/>
3. annotate the corresponding class.
@Repository persistence annotations.
@Service business layer notes
@Controller control layer notes
@Autowired automatic injection according to the type of help you automatically injected, if a plurality of the same type as it will in the injection by name. ( Recommended for this )
@Resouce automatically inject injection by name, if not the same name that will help you inject bean by type. It can assign a name to inject.

 

 

6. The spring incorporated in the file attribute profile

1 <context:property-placeholder location="classpath:my.properties"/>
2 <bean id="users" class="com.zhiyou100.spring.Users">
3     <!-- ${user.name}这个属性会打印电脑的用户名 -->
4     <property name="name" value="${users.name}"></property>
5     <property name="age" value="${users.age}"></property>
6     <property name="address" value="${users.address}"></property>
7 </bean>
users.name=zzzz
users.age=55
users.address=asdasd

测试结果为

 

 若使用user.name这个属性会打印电脑的用户名,将上面的代码不变,改变这一条后测试结果为

 

 

 

Guess you like

Origin www.cnblogs.com/murmansk/p/11478579.html