zoukankan      html  css  js  c++  java
  • 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);
        }
    }

    http://www.cnblogs.com/wenbronk/

  • 相关阅读:
    ArcGIS数据建模 (模型构建器modelbuilder) 培训视频 5章28小节587分钟视频 51GIS网站上线
    arcgis python ListEnvironments 函数可返回地理处理环境名称列表。
    arcgis python ValueTable使用
    解决ArcMap启动时只停留在初始化界面的方法
    Eutils用法总结
    EF 汇总函数使用注意事项Max()/Min()等
    C#多线程
    EF Attach时报错
    [Windows报错]要求的函数不受支持、这可能是由于 CredSSP 加密 Oracle 修正
    C#遍历XmlDocument对象所有节点名称、类型、属性(Attribute)
  • 原文地址:https://www.cnblogs.com/wenbronk/p/6848976.html
Copyright © 2011-2022 走看看