Getting Started with Spring Framework Analysis

A, Spring Bean configuration
in front of the class declaration entity class needs to be created objects Spring Framework annotated: @component. So that when Spring scan, see the notes will create the entity class objects in the container. So, Spring can scan to the class how to do? Under configuration folder, right -new-Spring Bean Configuration File, and then create the file, the file name can be, here set to "applicationContext.xml".
In applicationContext.xml need to be configured to automatically scan the Spring Framework package at the time of loading, the following code fragment is set by:

<!-- 配置自动扫描的包 
        base-package属性:
    -->
<context:component-scan base-package="com.spring.helloworld"></context:component-scan>

Meanwhile, a statement in our test class IOC container object is created when the specified load profile.

  1. IOC container to create objects
    created IOC container object while the object is created entity classes.
    Such as:
ApplicationContext ioc = new ClassPathXmlApplicationContext("applicationContext.xml");

This time the entity class object has been created. (No reference can be printed in the entity class constructor in a text, execution in the above test class code, output console will find no argument constructor print the text described in the same time to create a container object IOC It creates an object of the entity classes. Furthermore, the default object entity class is created in a single embodiment.

  1. Acquiring the entity class object IOC container

IOC container and then use the created object to remove the entity created a class object. The getBean method (), the method overload plurality,
obtaining manner bean
(1) obtained according to the name of the bean (id attribute value)
- the default object id IOC container class name is the first letter in lower case
(2) Depending on the type of bean acquisition
- Note: to ensure that the type of bean is unique within the IOC container, otherwise it will throw an exception
(3) obtained according to the bean name and type

Object getBean(String name)
<T> T getBean(String name, Class<T> requiredType)
<T> T getBean(Class<T> requiredType) 
Object getBean(String name, Object... args)

① getBean (String name)
Note method getBean (String name), a string argument to pass the class name first letter lowercase entity classes, such as class names are XxxYyyZzz, in order to get the object of this class, call the IOC container object when the method of the first class letter lowercase:

ioc.getBean(xxxYyyZzz);

Note that the return value of type Object, and therefore should use the present value if the entity class variable that receives the object before a "modeling" (cast). Such as:

HelloWorld helloWorld = (HelloWorld) ioc.getBean("helloWorld");

T getBean(Class requiredType)

When using this method, when directly into the mass participation category name .class can be, and the return type is the type of entity class, no style, but this method has a flaw, that is if the time of the Spring Framework configuration, instead of using annotation @Componentconfiguration but in the configuration file the applicationContext.xml (speaking there hereinbefore) configured by the tag, the configuration code fragment is as follows:

<bean id="helloWorld" class="com.spring.helloworld.HelloWorld" scope="prototype"</bean>

<bean id="helloWorld2" class="com.spring.helloworld.HelloWorld"></bean> 

As it can be seen from the configuration file, the arranged configuration when two HelloWorldobjects entity classes, respectively helloWorld, and helloWorld2
a result, when obtained in the test object HelloWorld class objects by IOC will appear abnormal, since there are two objects, it when the container does not know what to take.
Further, since the default configuration entity class object is created in a single embodiment, in order to become more cases, need to add annotations in the front @scope("prototype"), so that every time to get through the new ioc objects are created, instead of the same .

  1. Entity class method calls
    directly acquired . Method () object entity classes way to call.

Second, the annotation-based configuration Bean
. 1) Common components: @Component
identifies a component managed by Spring IOC container
2) Persistent Layer Components: @Repository
identifying a persistence layer Spring IOC container assembly managed by
3) business logic components: @Service
identifies a business logic component managed by Spring IOC container
4) presentation layer controller assembly: @Controller
identifies a presentation layer by the controller assembly Spring IOC container managed
5) assembly naming convention
simple components used: ① the default the resulting string class name as a lowercase first letter after the bean id
② using components annotation value attribute specifies the bean id

Note: Spring does not actually able to identify a component in the end it is not the type of mark, even with the @Respository notes in a presentation layer on top of the controller assembly will not have any errors, so @ Respository, @ Service, @ Controller these notes just to allow developers to define their own role to play in the current assembly.

By default, id objects created in IOC container is the class name first letter lowercase, we can be specified by the annotation id attribute value, attribute name and value can be omitted. Such as:

public interface UserDao {

    void saveUser();
}

@Repository("userDao1") 该实体类的id已经变为UserDao1
//@Repository(value="userDao") 经过该注解的设置后,该实体类的id为UserDao
public class UserDaoImpl implements UserDao {

    @Override
    public void saveUser() {
        System.out.println("UserDaoImpl正在向数据库中插入用户");
    }

}

If you want to make Spring to create objects of another class, and how to inject it in a class? Spring default is not injected, and then we can declare in front of another class annotate @Aurowired. Such as:

@Service("userService")
public class UserServiceImpl implements UserService {

    @Qualifier("userDaoImpl2")
//  @Qualifier(value="userDaoImpl2")
    @Autowired
    private UserDao userDao;
    
    /*
     * 默认情况下添加了@Autowired注解的属性必须注入成功,否则会抛出异常,我们通过设置该注解
     * 的required的属性值为false来告诉Spring该注解不是必须的,可以不注入
     */
    @Autowired(required=false)
    private User user;
    
    @Override
    public void saveUser() {
        //调用UserDao中保存用户信息的方法
        userDao.saveUser();
    }

}

By default, if you add a @Autowiredproperty notes, then must be injected successfully, otherwise it will throw an exception , we annotated by setting the requiredproperty to false to tell the Spring annotation is not required, it can not be injected. Such as:

@Autowire(required=false)

to sum up:

Spring dependency injection step (automatic assembly) of:

  1. Automatic assembly according to the type of bean
  2. Looking to the id attribute name to achieve automatic assembly of the container from the IOC
  3. If you can not achieve automatic assembly through 1 and 2, you can specify id automatically assembled by the value attribute @Qualifier annotation, value and attribute names can be omitted

Also, do not inject properties in the test class in the Spring! ! ! Why can consider the next ~ ~ ~

because,

If you can inject properties, OK, if the injection attribute success, then, Spring when scanning Test class, we add the purpose of notes is to allow Spring to us to create an object, when the creation IOC container object, it will automatically load reads the configuration file (this article is applicationContext.xml), while in the configuration file (applicationContext.xml) in which all the specified package is specified to be scanned, and then will go to scan Test class and then scan time Test class, found @Autowire annotations, Spring will be understood as the user wants to create an object, and then went to create the IOC, then reads the configuration file, and then ............ it never stops.

So, never let paranoia in Spring test class to inject your property! ! !

Guess you like

Origin www.cnblogs.com/zhqin/p/11729657.html