栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring:四维一体

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring:四维一体

在配置文件中写:

在配置Bean中写:@PropertySources: 是2.基于特定功能注解

 

 

基于schema: 

Account:

package com.baizhiedu.four;

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

import java.io.Serializable;

public class Account implements Serializable {
    @Value("${id}")
    private Integer id;
    @Value("${name}")
    private String name;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

applicationContext.xml:





   






    


    

    

    

four.properties:

id=10
name=xiaojianren

TestAnnotation3:

package com.baizhiedu;

import com.baizhiedu.bean.Customer;
import com.baizhiedu.config.AppConfig1;
import com.baizhiedu.four.Account;
import com.baizhiedu.injection.UserDAO;
import com.baizhiedu.injection.UserService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestAnnotation3 {
    //用于测试:四维一体
    @Test
    public void test1(){
        ApplicationContext ctx=new ClassPathXmlApplicationContext("/applicationContext.xml");
        Account account=(Account) ctx.getBean("account");
        System.out.println("account.getId="+account.getId());
        System.out.println("account.getIName="+account.getName());
    }


}

 test1:

 通过注解方式:

Account:

package com.baizhiedu.four;

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

import java.io.Serializable;
@Component //注解不用在配置文件中创建对象了
@PropertySource("classpath:four.properties") //注解扫描不用在配置文件中书写了
public class Account implements Serializable {
    @Value("${id}")
    private Integer id;
    @Value("${name}")
    private String name;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

注释掉applicationContext.xml中的创建对象和扫描的部分:





   






    


    
    




 

 上面那两种方式底层都是通过PropertySourcesPlaceholderConfiguer类来完成的:
























    

Account:

package com.baizhiedu.four;

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

import java.io.Serializable;
@Component //注解不用在配置文件中创建对象了
//@PropertySource("classpath:four.properties") //注解扫描不用在配置文件中书写了
public class Account implements Serializable {
    @Value("${id}")
    private Integer id;
    @Value("${name}")
    private String name;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

上面一样的测试方法:

 基于Bean注解方式:

applicationContext.xml:注解到包扫描和基于原始Bean标签
























AppConfig10:

package com.baizhiedu.four;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.io.ClassPathResource;

@Configuration //配置Bean
@ComponentScan(basePackages = "com.baizhiedu.four") //注解扫描,配置文件
public class AppConfig10 {
    @Bean
    public PropertySourcesPlaceholderConfigurer configurer(){
        PropertySourcesPlaceholderConfigurer configurer=new PropertySourcesPlaceholderConfigurer();
        //手工设置路径
        configurer.setLocation(new ClassPathResource("four.properties"));
        return configurer;
    }
}

TestAnnotation3:

package com.baizhiedu;

import com.baizhiedu.bean.Customer;
import com.baizhiedu.config.AppConfig1;
import com.baizhiedu.four.Account;
import com.baizhiedu.four.AppConfig10;
import com.baizhiedu.injection.UserDAO;
import com.baizhiedu.injection.UserService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestAnnotation3 {
    //用于测试:四维一体
    @Test
    public void test1(){
        ApplicationContext ctx=new ClassPathXmlApplicationContext("/applicationContext.xml");
        Account account=(Account) ctx.getBean("account");
        System.out.println("account.getId="+account.getId());
        System.out.println("account.getIName="+account.getName());
    }
    //测试基于Bean注解方式
    @Test
    public void test2(){
        ApplicationContext ctx=new AnnotationConfigApplicationContext(AppConfig10.class);
        Account account=(Account) ctx.getBean("account");
        System.out.println("account.getId="+account.getId());
        System.out.println("account.getIName="+account.getName());
    }


}

test2:

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/936179.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号