简单谈谈Spring的IOC与AOP

来源:互联网 发布:当前数据连接不可用 编辑:程序博客网 时间:2024/05/31 11:04

IOC即控制反转(Inversion Of Control--IOC),创建对象的控制权反转,也就是说原来创对象是由本身new出来的,现在通过IOC容器进行创建对象。

说到IOC就要说说DI即依赖注入(dependency injection--DI),依赖注入是通过IOC容器来进行创建对象和维护对象之间的关系,依赖注入的主要目的是为了解耦,下面我们用代码来说明一下,

使用的注解形式,其中

@Component 没有特殊的定义

@Service 一般是用在业务层

@Repository 一般使用在数据访问层

@Controller 一般使用在接入层(MVC)

@Autowired 

@Resource

接下来演示基于注解的形式对Bean的初始化和依赖注入,spring容器类选用AnnotationConfigApplicationContext

1.创建maven工程

依赖注入

引入依赖

<dependency>    <groupId>org.springframework</groupId>    <artifactId>spring-context</artifactId>    <version>4.0.2.RELEASE</version></dependency>

创建功能bean

import org.springframework.stereotype.Service;@Service
//该注解说明在spring容器中声明了一个bean,同理@Controller,@Repository,@Component的作用是一样的
public class service {    public String sayHello(String value){        return "hello" + value + "!";    }}

使用功能bean

import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;@Servicepublic class userService {    @Autowired//将service的实体类(bean)注入到userService中    private service service;    public void sayHello(String value){        System.out.println(service.sayHello(value));    }}


创建配置bean

import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;@Configuration
//该注解作用相当于<beans></beans>,声明该类是一个配置类
@ComponentScan("com.kingsoft.demo")
//该注解作用扫描demo包下所有的@Service,
@Controller,@Repository,@Component,注解的类,声明bean
public class serviceConfig {}

执行

import com.kingsoft.demo.service.userService;import org.springframework.context.annotation.AnnotationConfigApplicationContext;public class Test {    public static void main(String[] args){//初始化spring容器        AnnotationConfigApplicationContext context =                new AnnotationConfigApplicationContext(serviceConfig.class);        //初始化userService的实体bean        userService userService = context.getBean(userService.class);        //输出结果        System.out.println(userService.sayHello("World"));        //关闭资源        context.close();    }}

结果

D:\develop\jdk\bin\java "-javaagent:D:\develop\IntelliJ IDEA 2017.2.4\lib\idea_rt.jar=56637:D:\develop\IntelliJ IDEA 2017.2.4\bin" -Dfile.encoding=UTF-8 -classpath D:\develop\jdk\jre\lib\charsets.jar;D:\develop\jdk\jre\lib\deploy.jar;D:\develop\jdk\jre\lib\ext\access-bridge-64.jar;D:\develop\jdk\jre\lib\ext\cldrdata.jar;D:\develop\jdk\jre\lib\ext\dnsns.jar;D:\develop\jdk\jre\lib\ext\jaccess.jar;D:\develop\jdk\jre\lib\ext\jfxrt.jar;D:\develop\jdk\jre\lib\ext\localedata.jar;D:\develop\jdk\jre\lib\ext\nashorn.jar;D:\develop\jdk\jre\lib\ext\sunec.jar;D:\develop\jdk\jre\lib\ext\sunjce_provider.jar;D:\develop\jdk\jre\lib\ext\sunmscapi.jar;D:\develop\jdk\jre\lib\ext\sunpkcs11.jar;D:\develop\jdk\jre\lib\ext\zipfs.jar;D:\develop\jdk\jre\lib\javaws.jar;D:\develop\jdk\jre\lib\jce.jar;D:\develop\jdk\jre\lib\jfr.jar;D:\develop\jdk\jre\lib\jfxswt.jar;D:\develop\jdk\jre\lib\jsse.jar;D:\develop\jdk\jre\lib\management-agent.jar;D:\develop\jdk\jre\lib\plugin.jar;D:\develop\jdk\jre\lib\resources.jar;D:\develop\jdk\jre\lib\rt.jar;D:\workspace\Spring_IOC_AOP\target\classes;D:\develop\Repository\org\springframework\spring-context\4.0.2.RELEASE\spring-context-4.0.2.RELEASE.jar;D:\develop\Repository\org\springframework\spring-aop\4.0.2.RELEASE\spring-aop-4.0.2.RELEASE.jar;D:\develop\Repository\aopalliance\aopalliance\1.0\aopalliance-1.0.jar;D:\develop\Repository\org\springframework\spring-beans\4.0.2.RELEASE\spring-beans-4.0.2.RELEASE.jar;D:\develop\Repository\org\springframework\spring-core\4.0.2.RELEASE\spring-core-4.0.2.RELEASE.jar;D:\develop\Repository\commons-logging\commons-logging\1.1.3\commons-logging-1.1.3.jar;D:\develop\Repository\org\springframework\spring-expression\4.0.2.RELEASE\spring-expression-4.0.2.RELEASE.jar com.kingsoft.demo.run.Test
十月 31, 2017 10:50:50 上午 org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@4d405ef7: startup date [Tue Oct 31 10:50:50 CST 2017]; root of context hierarchy
helloWorld!
十月 31, 2017 10:50:50 上午 org.springframework.context.annotation.AnnotationConfigApplicationContext doClose
信息: Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@4d405ef7: startup date [Tue Oct 31 10:50:50 CST 2017]; root of context hierarchy


Process finished with exit code 0


java配置

功能bean

public class service {    public String sayHello(String value){        return "hello" + value + "!";    }}

使用功能bean

public class UserService {        private service service;        public void setService(service service){        this.service = service;    }    public String sayHello(String value){        return service.sayHello(value);    }}

配置类

@Configurationpublic class serviceConfig {    //声明service的实体bean    @Bean    public service getService(){        return new service();    }    //声明UserService的实体bean    @Bean    public UserService getUserService(){        UserService userService = new UserService();        userService.setService(getService());        return userService;    }    }
执行

public class Test {    public static void main(String[] args){        AnnotationConfigApplicationContext context =                new AnnotationConfigApplicationContext(serviceConfig.class);        UserService userService = context.getBean(UserService.class);        System.out.println(userService.sayHello("World!"));        context.close();    }}

结果

D:\develop\jdk\bin\java "-javaagent:D:\develop\IntelliJ IDEA 2017.2.4\lib\idea_rt.jar=56432:D:\develop\IntelliJ IDEA 2017.2.4\bin" -Dfile.encoding=UTF-8 -classpath D:\develop\jdk\jre\lib\charsets.jar;D:\develop\jdk\jre\lib\deploy.jar;D:\develop\jdk\jre\lib\ext\access-bridge-64.jar;D:\develop\jdk\jre\lib\ext\cldrdata.jar;D:\develop\jdk\jre\lib\ext\dnsns.jar;D:\develop\jdk\jre\lib\ext\jaccess.jar;D:\develop\jdk\jre\lib\ext\jfxrt.jar;D:\develop\jdk\jre\lib\ext\localedata.jar;D:\develop\jdk\jre\lib\ext\nashorn.jar;D:\develop\jdk\jre\lib\ext\sunec.jar;D:\develop\jdk\jre\lib\ext\sunjce_provider.jar;D:\develop\jdk\jre\lib\ext\sunmscapi.jar;D:\develop\jdk\jre\lib\ext\sunpkcs11.jar;D:\develop\jdk\jre\lib\ext\zipfs.jar;D:\develop\jdk\jre\lib\javaws.jar;D:\develop\jdk\jre\lib\jce.jar;D:\develop\jdk\jre\lib\jfr.jar;D:\develop\jdk\jre\lib\jfxswt.jar;D:\develop\jdk\jre\lib\jsse.jar;D:\develop\jdk\jre\lib\management-agent.jar;D:\develop\jdk\jre\lib\plugin.jar;D:\develop\jdk\jre\lib\resources.jar;D:\develop\jdk\jre\lib\rt.jar;D:\workspace\Spring_IOC_AOP\target\classes;D:\develop\Repository\org\springframework\spring-context\4.0.2.RELEASE\spring-context-4.0.2.RELEASE.jar;D:\develop\Repository\org\springframework\spring-aop\4.0.2.RELEASE\spring-aop-4.0.2.RELEASE.jar;D:\develop\Repository\aopalliance\aopalliance\1.0\aopalliance-1.0.jar;D:\develop\Repository\org\springframework\spring-beans\4.0.2.RELEASE\spring-beans-4.0.2.RELEASE.jar;D:\develop\Repository\org\springframework\spring-core\4.0.2.RELEASE\spring-core-4.0.2.RELEASE.jar;D:\develop\Repository\commons-logging\commons-logging\1.1.3\commons-logging-1.1.3.jar;D:\develop\Repository\org\springframework\spring-expression\4.0.2.RELEASE\spring-expression-4.0.2.RELEASE.jar com.kingsoft.demo.run.Test
十月 31, 2017 11:32:53 上午 org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@4d405ef7: startup date [Tue Oct 31 11:32:53 CST 2017]; root of context hierarchy
helloWorld!!
十月 31, 2017 11:32:53 上午 org.springframework.context.annotation.AnnotationConfigApplicationContext doClose
信息: Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@4d405ef7: startup date [Tue Oct 31 11:32:53 CST 2017]; root of context hierarchy


Process finished with exit code 0


AOP:面向切面编程,目的是为了解耦

实际应用推荐一篇文章:http://blog.csdn.net/u010987379/article/details/52152925


最后附注:本人小白一个,还是个学生,然后那里写的不好希望大神指导,我写博客的目的是为了帮助我自己去理解,去学习的,如有那里写错了,或者误导了某些兄弟姐妹,属实抱歉!!!

原创粉丝点击