引言:相信只要是做JAVA开发的朋友们都对Spring这个框架有所了解,这个框架的核心就是两个概念:IOC控制反转(又称依赖注入)和AOP(面向切面编程);今天自己就简单总结这端时间自己学习Spring的IOC理解;
一:什么是IOC?
Inversion of Control控制反转;主要想表达的就是将创建对象的过程交给了web容器;而不是有我们自己去new;具体理解见:http://www.cnblogs.com/xdp-gacl/p/4249939.html;
二:如何使用:
1:新建web工程后导入一些必要的jar包;
2:在我们项目的源文件夹src下面新建一个xml文件;
具体代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:ehcache="http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring
http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring-1.1.xsd"
default-lazy-init="false">
<!--表头是一些用来引入一些标签;获取自动提示-->
<description>Spring公共配置文件 </description>
<!--创建了user类的实例-->
<bean id="user1" class="com.chenpeng.user">
<property name="username" value="陈鹏"></property>
</bean>
</beans>
3:怎么去获取容器中的这个对象呢?
两种方式:
A:在main方法中:
ApplicationContext context = new FileSystemXmlApplicationContext("文件的绝对地址");
context.getBean("beanID");
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:xml文件名");
B:交给web容器自动初始化:
这种方式需要我们在web.xml中配置:
<context-param>
<!--这个参数的值是固定的-->
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
然后再在页面上去获取:
<%
//1:web.xml进行注册
WebApplicationContext context = (WebApplicationContext)application.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(application);
User cp= (User)context.getBean("chenpeng");
%>
4:1:IOC的对象类型注入方式:
A:构造函数注入
在类文件中写一个构造方法最好在类文件中留下默认的构造方法
private String name;
private String cls;
public Student(String name,String cls){
this.name = name;
this.cls = cls;
}
在相应的xml配置文件中写下:
<bean id="student1" class="com.chenpeng.Entity.Student">
<constructor-arg index="0" value="陈"></constructor-arg>
<constructor-arg index="1" value="软件12-1"></constructor-arg>
</bean>
```
B:属性注入
```
private String name;
private String cls;
public void setName(String name) {
this.name = name;
}
public void setCls(String cls) {
this.cls = cls;
}
在相应的xml配置文件中写下:
<bean id="student1" class="com.chenpeng.Entity.Student">
<property name="name" value="陈鹏"></property>
<property name="cls" value="软件12-1"></property>
</bean>
```
C:接口注入
```
public class ClassA {
private InterfaceB clzB;
public void doSomething() {
Ojbect obj =Class.forName(Config.BImplementation).newInstance();
ClassA 在编译期即依赖于 InterfaceB 的实现。为了将调用者与实现者在编译期分离,于是有了上面的代码.我们根据预先在配置文件中设定的实现类的类名 (Config.BImplementation) ,动态加载实现类,并通过 InterfaceB 强制转型后为 ClassA 所用。这就是接口注入的一个最原始的雏形。
clzB = (InterfaceB)obj;
clzB.doIt()
}
……
}
```
5:使用注解的方式进行初始化依赖的对象
@Component
通用的注解表示该类采用了注解
@Controller
用来注解控制层的
@Service
用来注解服务层
@Repository
用来注解数据路操作
@Configuration
采用类注入
@Qualifier
注解属性,可以在后面直接跟属性名字
@Autowired
在属性上面加了这行代码之后就需要再添加:set方法的;
在xml文件中添加一行扫包代码:
```
<!它会扫描对应的包, 并对添加了注解的类进行实例化>
<context:component-scan base-package="com.chenpeng.dao.**"/>
<context:component-scan base-package="com.chenpeng.service.**"/>
<context:component-scan base-package="com.chenpeng.web.**"/>
```
6:类注解:
@Configuration
采用类注入
关于类注解:
如果你不想使用set方法:可以通过实现一个BeanNameAware接口并且重写一个里面的方法来通过注解实现;
```
首先需要在一个统一的类类型的配置文件中写如下代码:
@Configuration//表示是一个类注解
public class daoConfig {
@Bean
public User getUser(){
User user = new User();
使用set方法注入
user.setId(1);
user.setUsername("陈鹏");
user.setPassword("11111");
return user;
}
@Bean
public UserDao getDao(){
UserDao userdao = new UserDao();
不使用set方法注入就采用实现接口BeanNameAware方式;
return userdao;
}
}
eg:
@Repository
public class UserDao implements BeanNameAware{
@Autowired
private User user;
public void say(){
System.out.println(user.getUsername());
}
@Override
public void setBeanName(String arg0) {
// TODO Auto-generated method stub
System.out.println("userDao注入了"+arg0);
}
}
```