Spring学习文档_IOC配置与应用

控制反转(IOC Inversion of Control)

依赖注入(Dependency Injection)

Samples:http://blog.springsource.com

采用XML

1.     注入类型

setter(重要)

<property name="userDAO" ref="userDao" />

构造方法(有印象就好)

<constructor-arg ref="userDao" />


2.     id vs. name -- id和name的意义相同.但是name可以用特殊字符

<bean name="userService" class="com.lohamce.service.UserServiceImpl"></bean>
<bean id="userService" class="com.lohamce.service.UserServiceImpl"></bean>


3.     简单属性的注入

<property name=… value=….>

4.     <bean 中的scope属性

singleton 单例 (默认情况下就是该属性)

proptotype 每次创建新的对象, 使用很少

5.     集合注入 collections 将一些属性放到一个集合里面, 很少用

6.     自动装配, 有一定的用处, 可以省略配置property,如果使用annotation,就会用到byType

byName: 根据属性名去查找

byType: 根据对象类型查找

如果所有的bean都用同一种,可以使用beans的属性:default-autowire 进行统一配置

7.     生命周期

lazy-init: 懒加载

init-method destroy-methd 不要和prototype一起用


采用Annotation: IOC的方面,Annotation比xml更方便

1.     Annotation第一步:查阅文档 Annotation-based configuration

修改beans.xml

<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 />


2.     @Autowired

默认按类型by type

<?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 />
  <bean id="u" class="com.lohamce.dao.impl.UserDAOImpl" />
	
  <bean name="userService" class="com.lohamce.service.impl.UserServiceImpl" />
  
</beans>

package com.lohamce.service.impl;

import org.springframework.beans.factory.annotation.Autowired;

import com.lohamce.dao.UserDAO;
import com.lohamce.model.User;
import com.lohamce.service.UserService;

public class UserServiceImpl implements UserService {
	private UserDAO userDAO;

	@Override
	public void add(User user) {
		userDAO.save(user);
	}

	public UserDAO getUserDAO() {
		return userDAO;
	}

	@Autowired
	public void setUserDAO(UserDAO userDAO) {
		this.userDAO = userDAO;
	}
}


如果想用byName,使用@Qulifier, 如果写在set上,@qualifier需要写在参数上

<?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 />
  <bean name="u2" class="com.lohamce.dao.impl.UserDAOImpl" />
	
  <bean name="userService" class="com.lohamce.service.impl.UserServiceImpl" />
  
</beans>

package com.lohamce.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import com.lohamce.dao.UserDAO;
import com.lohamce.model.User;
import com.lohamce.service.UserService;

public class UserServiceImpl implements UserService {
	private UserDAO userDAO;

	@Override
	public void add(User user) {
		userDAO.save(user);
	}

	public UserDAO getUserDAO() {
		return userDAO;
	}

	@Autowired
	public void setUserDAO(@Qualifier("u2")UserDAO userDAO) {
		this.userDAO = userDAO;
	}
}


3.  @Resource(重要),用到JSR-250, 现在是jcp (Java Community Press)来定制标准等

a)     加入:j2ee/common-annotations.jar

b)     默认byName查找,如果没有查找到,则byType

c)     可以指定特定名称

package com.lohamce.service.impl;

import javax.annotation.Resource;


import com.lohamce.dao.UserDAO;
import com.lohamce.model.User;
import com.lohamce.service.UserService;

public class UserServiceImpl implements UserService {
	private UserDAO userDAO;

	@Override
	public void add(User user) {
		userDAO.save(user);
	}

	public UserDAO getUserDAO() {
		return userDAO;
	}

	@Resource
	public void setUserDAO(UserDAO userDAO) {
		this.userDAO = userDAO;
	}
}



4.  @Component @Service @Controller @Repository,2.5.6版本上这四个annotation没有区别,用了这4个annotation的可以被用作resource

<context:component-scan base-package="***.***">

a)     初始化的名字默认为类名首字母小写

b)     可以指定初始化bean的名字


Component: 和Resource标签成套实用, 在注册Component的时候需要把名字写上

<?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.lohamce" />
  
</beans>

package com.lohamce.service.impl;

import javax.annotation.Resource;


import com.lohamce.dao.UserDAO;
import com.lohamce.model.User;
import com.lohamce.service.UserService;
@Component("userService")
public class UserServiceImpl implements UserService {
	private UserDAO userDAO;

	@Override
	public void add(User user) {
		userDAO.save(user);
	}

	public UserDAO getUserDAO() {
		return userDAO;
	}

	@Resource(name="userDAO")
	public void setUserDAO(UserDAO userDAO) {
		this.userDAO = userDAO;
	}
}

package com.lohamce.dao.impl;

import org.springframework.stereotype.Component;

import com.lohamce.dao.UserDAO;
import com.lohamce.model.User;

@Component("userDAO")
public class UserDAOImpl implements UserDAO {

	public void save(User user) {
		System.out.println("user saved!");
	}

}




5.  @Scope,默认singleton

package com.lohamce.service.impl;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;


import com.lohamce.dao.UserDAO;
import com.lohamce.model.User;
import com.lohamce.service.UserService;

@Scope("prototype")
@Component("userService")
public class UserServiceImpl implements UserService {
	private UserDAO userDAO;

	@Override
	public void add(User user) {
		userDAO.save(user);
	}

	public UserDAO getUserDAO() {
		return userDAO;
	}

	@Resource(name="userDao")
	public void setUserDAO(UserDAO userDAO) {
		this.userDAO = userDAO;
	}
}


6.  @PostConstruct = init-method; @PreDestroy = destroy-method;


Bean Scope(重要),参见文档:Bean Scope

Table 4.3. Bean scopes

Scope Description

singleton

(Default) Scopes a single bean definition to a single object instance per Spring IoC container.

prototype

Scopes a single bean definition to any number of object instances.

request

Scopes a single bean definition to the lifecycle of a single HTTP request; that is, each HTTP request has its own instance of a bean created off the back of a single bean definition. Only valid in the context of a web-aware Spring ApplicationContext.

session

Scopes a single bean definition to the lifecycle of an HTTP Session. Only valid in the context of a web-aware Spring ApplicationContext.

global session

Scopes a single bean definition to the lifecycle of a global HTTP Session. Typically only valid when used in a portlet context. Only valid in the context of a web-aware SpringApplicationContext.


Spring具备一个MVC框架,Spring MVC


自动装配(不太重要)

配置<bean autowire="">


Bean Lifcycle

1.<bean lazy-init="true">:使用到该class的时候才加载bean

2.<bean init-method="" destory-method="">:加载该bean的时候会调用的初始化方法以及销毁方法,只初始化一次


Java搜索引擎: Lucene

权限模块,工作流模块

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值