spring Diary (II)

A, spring-dependent manner injection

1. By way set

<bean id="hello" class="com.zhiyou100.cyf.bean.Hello" autowire="byType">
        <property name="name" value="${hello.name}"></property>
        <property name="age" value="${hello.age}"></property>
    </bean>

2. Constructor

<bean id="hello2" class="com.zhiyou100.cyf.bean.Hello">
        <constructor-arg index="0" value="辽宁" />
        <constructor-arg index="1" value="1" />

Second, the type of data dependency injection

  1. The basic data types and the character string using the value

  2. If a reference is a pointer to another object, use ref

<! - . Management: creating classes and attributes injection 
        bean: indicates the management class label 
        class: concrete class 
        id: labeling the tag 
        property in 
        if the attribute is: Sting and primitive type or packaging, by the value 
        if the attribute is: other pair of lines by REF 
        -> 
    < the bean ID = "MySQL" class = "com.zhiyou100.cyf.dao.MysqlUserDao" /> 
    < the bean ID = "Oracle" class = "com.zhiyou100.cyf.dao.OracleUserDao" /> 
    < the bean ID = "S" class = "com.zhiyou100.cyf.service.Service" > 
        < Property name = "mdao"property>
    </bean>

  3. If the property is injected list types, see pictures, there are labels in the property list, the inside

 

   4. If the map type, Property or tag map constructor has injected, wherein each entry keys and values ​​that may bean

<bean id="hello2" class="com.zhiyou100.cyf.bean.Hello">
        <constructor-arg index="0" value="辽宁" />
        <constructor-arg index="1" value="1" />
        <constructor-arg index="2">
            <map>
                <entry key="1" value-ref="stu1">
                </entry>
                <entry key="2" value-ref="stu2">
                </entry>
                <entry key="3" value-ref="stu3">
                </entry>
            </map>
        </constructor-arg>

    </bean>
    <bean id="stu1" class="com.zhiyou100.cyf.bean.Student">
        <property name="address" value="北京"></property>
    </bean>
    <bean id="stu2" class="com.zhiyou100.cyf.bean.Student">
        <property name="address" value="南京"></property>
    </bean>
    <bean id="stu3" class="com.zhiyou100.cyf.bean.Student">
        <property name="address" value="上海"></property>
    </bean>

Three, bean scopes

  The default is a singleton

Can be modified to the original mode, set the scope of prototype bean properties

<bean id="student" class="com.zhiyou100.cyf.bean.Student" scope="prototype">
        <property name="address" value="${user.name}"></property>
    </bean>

Fourth, the automatic injection. You may be provided to the bean property autowire byType or byName

byType: attribute type according to the class of the corresponding object to automatically inject it

byName: bean id to find the name of the class attribute in accordance with the respective object and infuse

<bean id="userController" class="com.zhiyou100.cyf.controller.UserController" autowire="byType"></bean>

 The default injection method may additionally disposed in the head tag

default-autowire="byName"

 

Fifth, the introduction of a property file in spring configuration file. And springmvc similar way to the introduction of

<! - . If more than one file to be introduced, are separated by commas, classpath: mo.properties 
        This version is later spring2.5 writing, before the writing of 
        
     <the bean class = "org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
         <Property = name "LOCATION" value = "CLASSPATH: the config.properties"> </ Property> 
     </ the bean> 
     -> 
    < context: Property-placeholder LOCATION = "CLASSPATH: the config.properties" />

Note that some key attributes of the file system may coincide with the variable name, such as user.name

Six ways to use annotations

  1. reintroduction jar package aop

  2. Pack scan profile using

<context:component-scan base-package="com.zhiyou100.cyf"></context:component-scan>

  3. In the corresponding class annotate

@Repository persistence annotations.

@Service business layer notes

@Controller control layer notes

These are equivalent to registration in the profile bean

Here is injected in the required properties

@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. (This is recommended)

@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.

More property name property resource can cooperate with byName, do not own the property name

controller Code

@Controller(value="userController")//设置id
public class UserController {
    @Autowired
    private UserService userService;
    public void selectById(int id) {
        userService.selectById(id);
    }
}

service codes

@Service
public class UserServiceImp implements UserService{
    @Autowired
    private UserDao userDao;
    @Override
    public void selectById(int id) {
        userDao.selectById(id);
    }

}

dao Code

@Repository
public class UserDaoImp implements UserDao{

    @Override
    public void selectById(int id) {
        System.out.println("根据id查询"+id);    
    }
}

 

Guess you like

Origin www.cnblogs.com/psxfd4/p/11478947.html