1,导入Spring核心开发包到创建工程
spring-beans-3.2.0.RELEASE.jar
spring-context-3.2.0.RELEASE.jar
spring-core-3.2.0.RELEASE.jar
spring-expression-3.2.0.RELEASE.jar
还需要下载commons-logging日志包
commons-logging-1.1.1.jar
2,素材:
1,HelloService.java
public class HelloService {
private String infomation;
public void say(){
System.out.println("待你长发及腰,"+infomation);
}
public void setInfomation(String infomation) {
this.infomation = infomation;
}
}
3,创建实例对象
1,配置文件 applicationContext.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"
xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="hello" class="com.yueshiwei.hello.HelloService">
<!-- 为实例对应的属性赋值, 要提供对应的set方法.. -->
<property name="infomation" value="程序员"></property>
</bean>
</beans>
2,通过Spring 框架解析配置文件
//加载classpath:
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
//加载磁盘路径:
//等效于ApplicationContext applicationContext=new FileSystemXmlApplicationContext("F:/workspace/heimaspring/src/applicationContext.xml");
HelloService helloService=(HelloService) applicationContext.getBean("hello");
//等效于 HelloService bean = applicationContext.getBean(HelloService.class);
bean.say();//待你长发及腰,黑马程序员
//等价于----------------------------------------------------
//通过bean工厂对管理的对象进行实例化..
BeanFactory factory = new XmlBeanFactory(new ClassPathResource("applicationContext.xml"));
HelloService helloServicess = (HelloService) factory.getBean("hello");
helloServicess.say();
============================================================================================================================
3,三种实例化Bean的方式
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
PersonService personService=(PersonService) factory.getBean("personService");
原有工厂
public class PersonServiceFactory {
public PersonService createPersonService(){
return new PersonServiceImpl();
}
}
1.使用类构造器实例化(默认无参数)
<bean id=“personService" class="cn.itcast.bean.impl.PersonServiceImpl"/>
2.使用静态工厂方法实例化(简单工厂模式)
<bean id="personService"
class="com.itcast.factory.PersonServiceFactory" factory-method="createPersonService" />
3.使用实例工厂方法实例化(工厂方法模式):
<bean id=“personServiceFactory" class="com.itcast.factory.PersonServiceFactory"/>
<bean id="personService" factory-bean=“personServiceFactory" factory-method="createPersonService" />
4,Bean的命名 id属性和name属性
一般情况下,装配一个Bean时,通过指定一个id属性作为Bean的名称
id 属性在IoC容器中必须是唯一的,id 的命名要满足XML对ID属性命名规范,必须以字母开始,可以使用字母、数字、连字符、下划线、句话、冒号
如果Bean的名称中含有特殊字符,就需要使用name属性
例如:<bean name="#person" class="cn.itcast.bean.Person"/>
因为name属性可以相同,所以后出现Bean会覆盖之前出现的同名的Bean
5,Spring初始化bean或销毁bean时,有时需要作一些处理工作,因此spring可以在创建和拆卸bean的时候调用bean的两个生命周期方法。
<bean id=“foo” class=“...Foo”
init-method=“setup”
destory-method=“teardown”/>
调用<bean init-method="init"> 指定初始化方法 init
调用<bean destroy-method="customerDestroy"> 指定销毁方法 customerDestroy
6,依赖注入Bean的属性
public class UserServlet extends HttpServlet {
private UserService service;//一定要记得加入set方法
private String username;
private int password;
public UserServlet(){
}
/** 通过够着函数给属性赋值...*/
public UserServlet(String username,int password){
this.username=username;
this.password=password;
}
/** 测试属性的值有没有被附上。。。*/
public void println(){
System.out.println("username="+username);
System.out.println("password="+password);
service.add();
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doPost(req, resp);
}
public void setUsername(String username) {
this.username = username;
}
public void setPassword(int password) {
this.password = password;
}
public void setService(UserService service) {
this.service = service;
}
}
1,构造函数注入
<bean id="userServlet" class="cn.itcast.action.UserServlet">
通过构造函数给指定的属性赋值...
<constructor-arg index="0" type="java.lang.String" value="张张" />
<constructor-arg index="1" type="int" value="1111" />
</bean>
2,属性setter方法注入
<bean id="userServlet" class="cn.itcast.action.UserServlet">
<property name="username" value="董小姐"></property>
<property name="password" value="123456"></property>
</bean>
3,使用<property>引入引用其他Bean
<bean id="userService" class="cn.itcast.action.UserServiceImpl">
<property name="name" value="董xx"></property>
</bean>
<bean id="userServlet" class="cn.itcast.action.UserServlet">
<property name="service" ref="userService"></property>
</bean>
4,集合类型属性注入
1,Map注入
public class Bean {
private Map<String, String> mapString;//往map 当中注入值...
public void setMapString(Map<String, String> mapString) {
this.mapString = mapString;
}
}
<bean id="bean" class="cn.itcast.bean.inject.Bean">
<property name="mapString">
<map>
<entry key="itcast" value="传智"></entry>
</map>
</property>
</bean>
2,List注入
public class Bean {
private List<String> stringlist;;//往list 当中注入值...
private String[] strs;;//往list 当中注入值...
public void setStringlist(List<String> stringlist) {
this.stringlist = stringlist;
}
public void setStrs(String[] strs) {
this.strs = strs;
}
}
<bean id="bean" class="cn.itcast.bean.inject.Bean">
<property name="stringlist">
<list>
<value>丽丽</value>
<value>笔笔</value>
</list>
</property>
<property name="strs">
<list>
<value>七七</value>
<value>奇奇</value>
</list>
</property>
</bean>
3,set注入
public class Bean {
private set<String> stringset;;//往list 当中注入值...
public void setStringset(set<String> stringset) {
this.stringset = stringset;
}
}
<bean id="bean" class="cn.itcast.bean.inject.Bean">
<property name="stringset">
<set>
<value>丽丽</value>
<value>奇奇</value>
</set>
</property>
</bean>
4,Properties
7,使用多个XML配置文件
方式一 可以在创建ApplicationContext对象时传入多个配置文件
ApplicationContext applicationContext = new
ClassPathXmlApplicationContext("beans1.xml", "beans2.xml");
方式二 可以在配置文件中通过<import>引入其他配置文件
<import resource="classpath:bean2.xml"/>
8,使用注解定义Bean
1,@Component 描述Spring框架中Bean
2,@Repository 用于对DAO实现类进行标注
3,@Service 用于对Service实现类进行标注
4,@Controller 用于对Controller实现类进行标注
//2,3,4三个注解是为了让标注类本身的用途清晰,Spring在后续版本会对其增强
5,@Autowired 默认按照类型进行注入,如果存在两个相同Bean类型相同,则按照名称注入
注入时可以针对成员变量或者setter方法
6,@Resource和@Autowired注解功能相似
<?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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd">
<!-- 打开注解的配置.. -->
<context:annotation-config></context:annotation-config>
<!-- 扫描指定的包与子包.. spring2.5的版本之后才出现的.. -->
<context:component-scan base-package="cn.itcast.bean.anotation"></context:component-scan>
</beans>
java...
1,--------------------------
@Service("userServiceImpl")
/**
* 加组件Compnent
* 相当于在配置文件当中加入了
* <bean id="useDao" class="cn.itcast.bean.anotation.UserDao">
* <bean id="userServiceImpl" class="cn.itcast.bean.anotation.UserServiceImpl">
* <property name="dao" ref="useDao"></property>
* </bean>
* 但在业务层我们一般加 @Service
*/
public class UserServiceImpl implements UserService {
//@Autowired //这个类是spring 提供的
@Resource(name="uDao")//resource 是javax 包扩展包提供的类..
private UserDao dao;
@PostConstruct//初始化
public void init(){
System.out.println("正在实例化...");
}
@PreDestroy//销毁
public void destory(){
System.out.println("正在销毁对象...");
}
@Override
public void add(String... ids) {
dao.add();
}
}
2,------------------------------
public interface UserService {
public void add(String...ids);
}
3,-------------------------------
@Repository("uDao")//在dao 层当中我们一般加 @Repository
public class UserDao {
public UserDao(){
}
public void add(){
System.out.println("dao 层add ");
}
public void deleteById(String...ids){
System.out.println(ids);
}
}
4,----------------------------------
@Test//测试。。。
public void testAnnotation(){
ClassPathXmlApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext1.xml");
/** 通过制定类型,制定的bean 的id 去获取bean 的实例,相当于不用强制转换.....*/
UserService userService=applicationContext.getBean("userServiceImpl",UserService.class);
userService.add("1");
applicationContext.close();//调用此方法,关闭工厂,相当于会触发bean 的销毁的方法...
}
//正在实例化...dao 层add 正在销毁对象...