How Spring uses annotations

one. configure pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.xingxue.spring</groupId>
    <artifactId>spring.day1</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <!-- Spring related dependencies -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.0.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>4.0.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>4.0.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>4.0.4.RELEASE</version>
        </dependency>
        <!-- The spring web module provides a listener to start the spring container -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>4.0.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.0.4.RELEASE</version>
        </dependency>
    </dependencies>

</project>

Second, configure beans.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd">                     


    <!-- component-scan tag, component scan tag:
                  base-package attribute: specify the scan package, scan the classes in all descendant packages of the specified package, and incorporate these classes into the spring container, so there is no need for bean label to indicate.
                                    Not all classes are scanned, only the marked classes are scanned:
                                     @Controller
                                     @Service
                                     @Repository
                                     @Component Just use one of the annotations
     -->
 < context :component-scan base-package ="com.xingxue" > </ context :component-scan >   

</beans>

three. Using annotations to configure classes

package com.xingxue.spring.web.controller;

import com.xingxue.spring.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

@Controller /* Choose one of those annotations*/
public class UserController {

    @Autowired/* This annotation is that spring can automatically help you omit the setter/getter method of the object referenced in the bean,
    It will automatically set/get for you. */
    private IUserService userService;
    public void login(){
        userService.login();
    }
}




package com.xingxue.spring.service;

import org.springframework.stereotype.Service;

@Service
public interface IUserService {
    public void login();
}



package com.xingxue.spring.service;

import com.xingxue.spring.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;

@Service(value = "userService") /*id attribute of bean tag configuration*/

public class UserService implements IUserService {

    @Autowired
    private UserDao userDao;



    public void login() {
        System.out.println("userservice method executed.");
        userDao.login();
    }
}
package com.xingxue.spring.dao;

import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;

@Repository(value = "userDao")
public class UserDao {
    public void login() {
        System.out.println("Querying....");
    }
}


Test effect

package com.xingxue.spring.test;

import com.xingxue.spring.beans.A;
import com.xingxue.spring.beans.B;
import com.xingxue.spring.web.controller.UserController;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App {

    public static void main(String[] args) {
        ApplicationContext cxt  = new ClassPathXmlApplicationContext("beans.xml");


       UserController userController= (UserController) cxt.getBean("userController");

        userController.login();

    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324693475&siteId=291194637