VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > 编程开发 > Java教程 >
  • 040_Spring注解开发

Spring注解开发

image.png

<?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
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">

    <!--注解支持-->
    <context:annotation-config/>

</beans>

bean注册到Spring容器中

applicationContext.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
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">

    <!--注解支持-->
    <context:annotation-config/>
    <!--指定要扫描的包,包下的注解就会生效-->
    <context:component-scan base-package="com.qing"/>

</beans>

实体类添加注解@Component

package com.qing.dao;

import org.springframework.stereotype.Component;

@Component
public class User {
    private String name;
}

import com.qing.dao.User;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {
    @Test
    public void test01(){
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        User user = context.getBean("user", User.class);
        System.out.println(user.getName());
    }
}

image.png
User实体类被Spring管理,通过@Component注解注册到Spring容器中

属性注入

属性添加注解@Value("张三丰")

package com.qing.pojo;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class User {
    @Value("张三丰")
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

import com.qing.pojo.User;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {
    @Test
    public void test01(){
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        User user = context.getBean("user", User.class);
        System.out.println(user.getName());
    }
}

image.png

@Component的衍生注解

在web开发中,会按照MVC三层架构分层,分别使用注解,但他们的作用和@Component注解是一样的,都是将类注册到spring容器中,只是为了标记分层使用不同的注解

dao层 @Repository

package com.qing.dao;

import org.springframework.stereotype.Repository;

@Repository
public class UserDao {
}

service层 @Service

package com.qing.service;

import org.springframework.stereotype.Service;

@Service
public class UserService {
}

controller层 @Controller

package com.qing.controller;

import org.springframework.stereotype.Controller;

@Controller
public class UserController {
}

自动装配

image.png

作用域

单例@Scope("singleton") 原型@Scope("prototype")

package com.qing.pojo;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@Component
@Scope("singleton")
//@Scope("prototype")
public class User {
    @Value("张三丰")
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

xml与注解区别

image.png

@Configuration 不使用Spring的xml配置,完全Java来做

JavaConfig是Spring的一个子项目,在Spring4之后,它成为了一个核心功能

添加配置类

@Configuration代表这是一个配置类

package com.qing.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

// @Configuration代表这是一个配置类,相当于applicationContext.xml
@Configuration
// @ComponentScan 扫描包下注解
@ComponentScan("com.qing.pojo")
// @Import 导入其他配置类
@Import(QingConfig2.class)
public class QingConfig {
}

实体类添加注解

package com.qing.pojo;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class User {
    @Value("张三丰")
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

测试

import com.qing.config.QingConfig;
import com.qing.pojo.User;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class MyTest {
    @Test
    public void test01(){
        // 通过AnnotationConfigApplicationContext获取容器,通过配置类的class加载
        ApplicationContext context = new AnnotationConfigApplicationContext(QingConfig.class);
        User user = context.getBean("user", User.class);
        System.out.println(user.getName());
    }
}

image.png
 

出处:https://www.cnblogs.com/wl3pb/p/15145775.html


相关教程