首页 > 代码库 > springboot-2-ioc

springboot-2-ioc

在spring环境下, ioc(控制反转 和 DI (依赖注入) 是等效的, 主要体现一种组合的松耦合思想. spring Ioc容器负责创建Bean, 并将Bean注入到所需的Bean中, 有xml, 注解, java配置, groovy配置等实现

 

声明bean的注解有: 

@Component, 没有角色
@Controller, 为mvc的展现层
@RestController, springboot中使用, 相当于 @Controller和@ResponseBody
@Service, 为Service层
@Repositoy, 为数据交互层

使用bean的注解有: 

@Autowired
@Resource: JSR-250
@Inject: JSR-330

1, 使用注解声明的bean的使用

这种方式比较简单, 加个注解, spring就会根据aop注解去判断并加载到context中

TestService
package com.wenbronk.config.annotation;

import org.springframework.stereotype.Service;

/**
 * Created by wenbronk on 2017/5/12.
 */
@Service
public class TestService {

    public String sayHello(String hello) {
        return "Hello" + hello;
    }

}

TestController

package com.wenbronk.config.annotation;

import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * Created by wenbronk on 2017/5/12.
 */
@Service
public class TestController {

    @Resource
    private TestService testService {
  
  public String sayHello(String word) { return testService.sayHello(word); } }

Config配置, 使用@SpringBootConfiguration配置, 声明当前类为一个配置类

package com.wenbronk.config.annotation;

import org.springframework.boot.SpringBootConfiguration;
import org.springframework.context.annotation.ComponentScan;

/**
 * Created by wenbronk on 2017/5/12.
 */
@SpringBootConfiguration
@ComponentScan(value = {"com.wenbronk.config.annotation"})
public class AnnotationConfig {

}

Test

package com.wenbronk.config.annotation;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * Created by wenbronk on 2017/5/12.
 */

public class TestAnnotation {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AnnotationConfig.class);
        TestController user = context.getBean(TestController.class);
        String sayHello = user.sayHello("你好");
        System.out.println(sayHello);
    }
}

2, 使用java进行配置

关键在于config配置, 要保证使用的每个bean在config类下都有@Bean加入到context中

1), TestService, TestController, 

和上面的一样, 去掉 @Service就好...

2), javaconfig, 使用@Bean标签注入到context中

package com.wenbronk.config.java;

import org.springframework.boot.SpringBootConfiguration;
import org.springframework.context.annotation.Bean;

/**
 * 除了使用上面那种方法, 还可以直接new
 * Created by wenbronk on 2017/5/12.
 */
@SpringBootConfiguration
public class JavaConfig2 {

    @Bean
    public FunctionService functionService() {
        return new FunctionService();
    }

/** * spring容器中有的bean, 可以直接作为参数传入 * @param functionService * @return */ @Bean public UserFunctionService userFunctionService(FunctionService functionService) { UserFunctionService userFunctionService = new UserFunctionService(); userFunctionService.setFunctionService(functionService); return userFunctionService; } }

spring容器提供一个好处, 在context中管理的bean, 可通过形参的形式直接注入

 

3), Test

package com.wenbronk.config.java;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * Created by root on 2017/5/12.
 */
public class TestJava {

    public static void main(String[] args ) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(JavaConfig2.class);
        TestController bean = context.getBean(TestController.class);
        String hello = bean.sayHello("hello");
        System.out.println(hello);
    }
}

springboot-2-ioc