首先,在xml其中新增部分標有下劃線的文件,容器初始化的時候需要掃描包
?注意:
a.?????包款掃描(下劃線部分)一定要加,默認是不掃描整個包。與每一包之間’,’開。如過具有同樣的父包,那么我們能夠用父包來取代。例如以下劃線部分,我們能夠用com.bjsxt來取代。
<?
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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <context:annotation-config /><!-- 自己主動裝配一定要 加上此片段--> <context:component-scan base-package="com.bjsxt.dao.impl,com.bjsxt.service,com.bjsxt.model"></context:component-scan> </beans>
b.?????在2.5版本號中(@Component
、@Repository
、@Service
和@Controller
)四個標簽代表同樣的含義,以后的版本號中可能會有差別。
c.????
在標注組件等資源時候,盡量加上名稱比如@Component("stuDaoImpl")
。
默認的名稱是 類名首字母小寫。
<pre name="code" class="java">package com.bjsxt.dao.impl;
import javax.annotation.Resource;
import org.springframework.stereotype.Component;
import com.bjsxt.dao.*;
import com.bjsxt.model.Student;
@Component("stuDaoImpl")
public class StudentDaoImpl implements StudentDao{@Overridepublic void StudentSave(Student s) {System.out.println("學生被保存!");}
}
d.?????
用法 在set方法或者屬性名前增加 @resource(name="stuDaoImpl"),推薦增加名稱,默認是依照類型進行查找。比如:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;import com.bjsxt.dao.*;
import com.bjsxt.dao.impl.*;
import com.bjsxt.model.*;
@Component("studentService")//聲明資源
public class StudentService {
private StudentDao studentDao;
public StudentDao getStudentDao() {return studentDao;
}
@Resource(name="stuDaoImpl")//注入
public void setStudentDao(StudentDao studentDao) {this.studentDao = studentDao;
}
public void add(Student s)
{this.studentDao.StudentSave(s);
}
e.測試方式依舊和之前的注入方式一樣,注意這里的
Studentstudent=(Student)ctx.getBean("student");
是我們用標簽在student類中聲明的@Component("student")。
Spring容器會能通過ctx(相當于beanFactory)找到。
package com.bjsxt.service;import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;import com.bjsxt.model.Student;public class StudentServiceTest {@Testpublic void test() {System.out.println("程序執行之前....");ApplicationContext ctx=new ClassPathXmlApplicationContext("beans.xml");System.out.println("程序執行開始....");StudentService sService=(StudentService)ctx.getBean("studentService");Student student=(Student)ctx.getBean("student");Student student2=(Student)ctx.getBean("student");System.out.println(student2==student);sService.add(student);}
}
f.??
在jsp頁面須要處理業務,有java代碼須要spring注入service。
須要如今想要獲取的類前加標簽
@Component("docrelationService")首先jsp頁面導入
<%@page import="org.springframework.context.ApplicationContext"%>
<%@pageimport="org.springframework.web.context.support.WebApplicationContextUtils"%>
獲取spring注入
<%
????????ApplicationContext context =WebApplicationContextUtils
?????????????.getWebApplicationContext(application);
???????????DocrelationServicedocrelationService = (DocrelationService) context
?????????????.getBean("docrelationService");
%>
g.??
?假如類A由spring容器管理,在類B中引用A,假設想在B中的A是由spring容器創建的,有兩種方法:
?????a).類B也由spring容器管理(即類B前有@compoment(“b”)標簽),并注入A,用BeanFactory.getBean("b") 得到B實例,就可以(推薦).
?????b).類B不由spring容器管理,在類B中用代碼?(A)BeanFactory.getBean("a")得到A實例,就可以.(不推薦,會產生兩個beanFactory由于在類B中須要用BeanFactory,所以我們必須在B中new一個)
c)類B創建實例時候假設採用a)方法,決不能採用B?b=new?B();的方式,否則會報nullpoint異常。
d)ClassPathXmlApplicationContext建立在beanFactory基礎之上,非常少有人直接使用。
測試代碼:
package com.bjsxt.service;import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;import com.bjsxt.model.Student;public class StudentServiceTest {@Testpublic void test() {System.out.println("程序執行之前....");//BeanFactory ctx=new ClassPathXmlApplicationContext("beans.xml");//ClassPathXmlApplicationContext建立在beanFactory基礎之上,非常少有人直接使用。
ApplicationContext ctx=new ClassPathXmlApplicationContext("beans.xml");//直接獲取spring容器 System.out.println("程序執行開始...."); StudentService sService=(StudentService)ctx.getBean("studentService");//StudentService相當于類B //StudentService sService=new StudentService(); Student student=(Student)ctx.getBean("student"); Student student2=(Student)ctx.getBean("student"); System.out.println(student2==student); sService.add(student); } }
版權聲明:本文博客原創文章,博客,未經同意,不得轉載。