引言:相信只要是做JAVA開(kāi)發(fā)的朋友們都對(duì)Spring這個(gè)框架有所了解,這個(gè)框架的核心就是兩個(gè)概念:IOC控制反轉(zhuǎn)(又稱依賴注入)和AOP(面向切面編程);今天自己就簡(jiǎn)單總結(jié)這端時(shí)間自己學(xué)習(xí)Spring的IOC理解;
一:什么是IOC?
Inversion of Control控制反轉(zhuǎn);主要想表達(dá)的就是將創(chuàng)建對(duì)象的過(guò)程交給了web容器;而不是有我們自己去new;具體理解見(jiàn):http://www.cnblogs.com/xdp-gacl/p/4249939.html;
二:如何使用:
1:新建web工程后導(dǎo)入一些必要的jar包;
2:在我們項(xiàng)目的源文件夾src下面新建一個(gè)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">
<!--表頭是一些用來(lái)引入一些標(biāo)簽;獲取自動(dòng)提示-->
<description>Spring公共配置文件 </description>
<!--創(chuàng)建了user類的實(shí)例-->
<bean id="user1" class="com.chenpeng.user">
<property name="username" value="陳鵬"></property>
</bean>
</beans>
3:怎么去獲取容器中的這個(gè)對(duì)象呢?
兩種方式:
A:在main方法中:
ApplicationContext context = new FileSystemXmlApplicationContext("文件的絕對(duì)地址");
context.getBean("beanID");
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:xml文件名");
B:交給web容器自動(dòng)初始化:
這種方式需要我們?cè)趙eb.xml中配置:
<context-param>
<!--這個(gè)參數(shù)的值是固定的-->
<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>
然后再在頁(yè)面上去獲?。?/p>
<%
//1:web.xml進(jìn)行注冊(cè)
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的對(duì)象類型注入方式:
A:構(gòu)造函數(shù)注入
在類文件中寫(xiě)一個(gè)構(gòu)造方法最好在類文件中留下默認(rèn)的構(gòu)造方法
private String name;
private String cls;
public Student(String name,String cls){
this.name = name;
this.cls = cls;
}
在相應(yīng)的xml配置文件中寫(xiě)下:
<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;
}
在相應(yīng)的xml配置文件中寫(xiě)下:
<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 的實(shí)現(xiàn)。為了將調(diào)用者與實(shí)現(xiàn)者在編譯期分離,于是有了上面的代碼.我們根據(jù)預(yù)先在配置文件中設(shè)定的實(shí)現(xiàn)類的類名 (Config.BImplementation) ,動(dòng)態(tài)加載實(shí)現(xiàn)類,并通過(guò) InterfaceB 強(qiáng)制轉(zhuǎn)型后為 ClassA 所用。這就是接口注入的一個(gè)最原始的雛形。
clzB = (InterfaceB)obj;
clzB.doIt()
}
……
}
```
5:使用注解的方式進(jìn)行初始化依賴的對(duì)象
@Component
通用的注解表示該類采用了注解
@Controller
用來(lái)注解控制層的
@Service
用來(lái)注解服務(wù)層
@Repository
用來(lái)注解數(shù)據(jù)路操作
@Configuration
采用類注入
@Qualifier
注解屬性,可以在后面直接跟屬性名字
@Autowired
在屬性上面加了這行代碼之后就需要再添加:set方法的;
在xml文件中添加一行掃包代碼:
```
<!它會(huì)掃描對(duì)應(yīng)的包, 并對(duì)添加了注解的類進(jìn)行實(shí)例化>
<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
采用類注入
關(guān)于類注解:
如果你不想使用set方法:可以通過(guò)實(shí)現(xiàn)一個(gè)BeanNameAware接口并且重寫(xiě)一個(gè)里面的方法來(lái)通過(guò)注解實(shí)現(xiàn);
```
首先需要在一個(gè)統(tǒng)一的類類型的配置文件中寫(xiě)如下代碼:
@Configuration//表示是一個(gè)類注解
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方法注入就采用實(shí)現(xiàn)接口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);
}
}
```