`
wiselyman
  • 浏览: 2081066 次
  • 性别: Icon_minigender_1
  • 来自: 合肥
博客专栏
Group-logo
点睛Spring4.1
浏览量:81086
74ae1471-94c5-3ae2-b227-779326b57435
点睛Spring MVC4...
浏览量:130134
社区版块
存档分类
最新评论

02点睛Spring4.1-Java Config

 
阅读更多

2.1 java config

  • spring的java config主要使用@Configuration@Bean两个注解;
    • 使用@Configuration注解在类上声明是一个配置类(相当于一个spring的配置xml);
    • 使用@Bean注解在方法上,返回值是一个类的实例,并声明这个返回值是spring的一个bean,bean的name是方法名;

2.2 关于@Bean和@Component,@Service,@Repository,@Controller

  • @Component,@Service,@Repository,@Controller注解在一个类上之后,这个类也成为spring容器中的bean,使用@Bean注解也是,感觉使用@Bean注解是不是更麻烦呢?

  • 既然效果是等同的,那什么时候使用@Bean什么时候使用@Component,@Service,@Repository,@Controller系列呢?

  • 这个原则就和我们当初混用xml配置和@Component,@Service,@Repository,@Controller时候一样:系统的全局配置(数据库配置,spring mvc配置,spring security配置等)使用java config(xml),业务相关的bean使用@Component,@Service,@Repository,@Controller系列。

  • 在后面我们讲到一些全局配置的时候我们就会使用Spring的java config

2.3 演示

2.3.1 创建一个properties(test.properties)文件作为配置

wisely.word = World

2.3.2 创建一个java class

package com.wisely.javaconfig;

public class DemoService {
    private String word;

    public String getWord() {
        return word;
    }

    public void setWord(String word) {
        this.word = word;
    }

    public String sayHello(){
        return "Hello "+this.word;
    }

}

2.3.3 创建java config配置类

package com.wisely.javaconfig;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;

@Configuration //声明是一个配置类
@PropertySource("com/wisely/javaconfig/test.properties")
public class DemoConfig {

    @Bean //声明是一个bean
    public DemoService demoBean(Environment environment){
        DemoService demoService = new DemoService();
        demoService.setWord(environment.getProperty("wisely.word"));
        return demoService;
    }
}

2.3.4 测试-初始化spring容器

package com.wisely.javaconfig;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Main {

    public static void main(String[] args) {
        //设定此包下的类被注册成spring的bean,
        //包含@Configuration,@Component,@Service,@Repository,@Controller
        AnnotationConfigApplicationContext context =
                            new AnnotationConfigApplicationContext("com.wisely.javaconfig");
        DemoService demoService = context.getBean(DemoService.class);
        System.out.println(demoService.sayHello());
        context.close();
    }

}

输出结果:Hello World

 

新书推荐《JavaEE开发的颠覆者: Spring Boot实战》,涵盖Spring 4.x、Spring MVC 4.x、Spring Boot企业开发实战。

 

京东地址:http://item.jd.com/11894632.html

当当地址:http://product.dangdang.com/23926195.html

亚马逊地址:http://www.amazon.cn/图书/dp/B01D5ZBFUK/ref=zg_bsnr_663834051_6 

淘宝地址:https://item.taobao.com/item.htm?id=528426235744&ns=1&abbucket=8#detail

 

 

 

或自己在京东、淘宝、亚马逊、当当、互动出版社搜索自选。

 


1
0
分享到:
评论
2 楼 wiselyman 2015-05-27  
QuarterLifeForJava 写道
楼主,关于Environment的写法我不知道是不是我的姿势不对~
它报错:
org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.core.env.Environment]: Specified class is an interface

但是这样确是可以的:
@Autowired
private Environment env;

然后在方法中调用:
//测试environment
String evo = env.getProperty("jdbcDriverClassName");


DemoConfig 
这个类的完整代码贴一下
1 楼 QuarterLifeForJava 2015-05-27  
楼主,关于Environment的写法我不知道是不是我的姿势不对~
它报错:
org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.core.env.Environment]: Specified class is an interface

但是这样确是可以的:
@Autowired
private Environment env;

然后在方法中调用:
//测试environment
String evo = env.getProperty("jdbcDriverClassName");

相关推荐

Global site tag (gtag.js) - Google Analytics