Spring 注解回顾

2 篇文章 0 订阅
1 篇文章 0 订阅

在回顾ssm各个注解前,我们先对xml中配置的下面三个内容做简单回顾:

<context:annotation-config/>

<context:component-scan/>

<mvc:annotation-driven/>

1.<context:annotation-config/> 隐式地向 Spring容器注册这4个BeanPostProcessor :

AutowiredAnnotationBeanPostProcessor
    @Autowired
RequiredAnnotationBeanPostProcessor
    @Required
CommonAnnotationBeanPostProcessor
    @Resource 、@PostConstruct、@PreDestroy
PersistenceAnnotationBeanPostProcessor
    @PersistenceContext

<context:annotation-config/>是用来使上述注解起作用的,也就是说激活已经在application context中注册的bean。之所以这样说是因为<context:annotation-config />仅能够在已经注册过的bean上面起作用。对于没有在spring容器中注册的bean,它并不能执行任何操作, 也就是说如果你并没有在spring容器中注册过bean(spring配置文件中配置bean就是注册(也就是说spring中不存在你想使用的bean)),那么上述的那些注解并不会在你未注册过的bean中起作用, 注意这些属性一般都是注册在bean中的字段或者方法上;

2.<context:component-scan base-package="com.zit,com.qian.dao"/>

<context:component-scan>做了<context:annotation-config>要做的事情,还额外支持@Component,@Repository,@Service,@Controller @RestController, @ControllerAdvice, and @Configuration 注解(注意这些注解一般都是注解在class上)。并且<context:component-scan>扫描base-package时会把扫描到的使用注解bean在application context中进行注册。也就是<context:component-scan>该注解可以扫描并注册你使用注解诸如@controller @service @component..的bean!!!

所以配置<context:component-scan>就不需要配置<context:annotation- config/>

3. <mvc:annotation-driven/>

对应的实现类为:org.springframework.web.servlet.config.AnnotationDrivenBeanDefinitionParser

<mvc:annotation-driven/>隐式的向beanFactory中注册如下类

用于处理请求映射的
    其中第一个是处理@RequestMapping注解的。
    第二个会将controller类的名字映射为请求url。
RequestMappingHandlerMapping
BeanNameUrlHandlerMapping

中间三个是用来处理请求的
    第一个处理@Controller注解的处理器,支持自定义方法参数和返回值(很酷)。
    第二个是处理继承HttpRequestHandler的处理器。
    第三个处理继承自Controller接口的处理器。
RequestMappingHandlerAdapter
HttpRequestHandlerAdapter
SimpleControllerHandlerAdapter

后面三个是用来处理异常的解析器
ExceptionHandlerExceptionResolver
ResponseStatusExceptionResolver
DefaultHandlerExceptionResolver 

此外还支持如下功能:

① 支持使用ConversionService实例对表单参数进行类型转换;
② 支持使用@NumberFormatannotation、@DateTimeFormat注解完成数据类型的格式化;
③ 支持使用@Valid注解对Java bean实例进行JSR 303验证;
④ 支持使用@RequestBody和@ResponseBody注解

上述内容整理自以下几篇博文:

https://blog.csdn.net/j080624/article/details/60883328

https://blog.csdn.net/j080624/article/details/60883328

关于注解的辅助知识:

首先,spring在读取到注解标注的 Bean 时,如果Bean没有命名,就会自动将 bean 的类名的首字符转成小写,作为Bean的 id;

其次,关于Spring bean的 name  和 id 的几点说明:

(1)每个Bean可以有一个id属性,并可以根据该id在IoC容器中查找该Bean,该id属性值必须在IoC容器中唯一;

(2)如果不指定id,只指定name,那么name为Bean的标识符,并且需要在容器中唯一;

(3)同时指定name和id,此时id为标识符,而name为Bean的别名,两者都可以找到目标Bean;

(4)可以指定多个name,之间可以用分号、空格或逗号隔开,如果没有指定id,那么第一个name为标识符,其余的为别名;若指定了id属性,则id为标识符,所有的name均为别名;

(5)在xml配置文件中:如果id和name都没有指定,则用类全名作为name,如<bean class="com.stamen.BeanLifeCycleImpl">,则你可以通过getBean("com.stamen.BeanLifeCycleImpl")返回该实例;

(6)在xml配置文件中:如果存在多个id和name都没有指定,且 实例类 都一样的,例如:<bean class="com.stamen.BeanLifeCycleImpl"> <bean class="com.stamen.BeanLifeCycleImpl">,则第一个bean通过getBean(“com.stamen.BeanLifeCycleImpl”) 获得,第二个bean通过getBean(“com.stamen.BeanLifeCycleImpl#1”)获得,以此类推。

(7)在注解配置中:如果不指定bean的name,默认将类名首字母小写作为类的名字

  • 注解《1》@Autowired

Autowired注解是Spring在2.5之后引入的,完成对类的 成员变量, set方法, 和构造函数进行自动注入,目的是减少类中出现的get set方法;

Autowired默认按类型注入, 当一个借口有多个实现类时,如何注入:通过如下示例进行说明:

// step1: 定义一个接口
public interface ImportService {
	void importInfo();
}

// step2: 定义接口的两个实现类和一个继承子类

/**
* 使用@Service注解标注,spring会自动管理这个Bean,并将
* 类名的首字母小写(xlsImportService)作为这个Bean的id;
* 也就是我们可以通过ctx.getBean("xlsImportService")来
* 找到这个bean;也可以通过@Resource("xlsImportService")
* 来自动注入这个bean
*/
@Service
public class XlsImportService implements ImportService {
	@Override
	public void importInfo() {
		System.out.println("XlsImportService");
	}
}

@Service
public class XlsImportServiceSon extends XlsImportService {
	@Override
	public void importInfo() {
		System.out.println("XlsImportServiceSon");
	}
}

@Service
public class TxtImportService implements ImportService {
	@Override
	public void importInfo() {
		System.out.println("TxtImportService");
	}
}

//step 3 建一个类来测试Autowried自动注入;

@Component
public class ComponentTest {
    /**
    * 失败:
    * 第一种注入方式:Autowired 注意此处引用名称为接口名首字母小写;
    * 报错:No qualifying bean of type [ImportService] is defined
    * 因为ImportService接口有三个子类,所以Spring通过类型注入时,
    * 不知道注入那个;
    */  
    @Autowired
    private ImportService importService;

    /**
    * 成功:
    * 第二种注入方式: Autowired 引用名称为某一个实现名首字母小写
    * 成功注入:TxtImportService实现;
    * 所以:当应用名字默认为接口实现ServiceBean的id名使,自动注入成功
    */
    @Autowired
    private ImportService txtImportService;
    
    /**
    * 成功:
    * 第三种注入方式: Autowired Qualifier("beanName")
    * 成功注入:XlsImportService实现;
    * 所以:当使用Autowired+Qualifier时,自动注入Qualifier中的beanName,
    * 与 接口后的引用名字txtImportService没有任何关系;
    */
    @Autowired
    @Qualifier("xlsImportService")
    private ImportService txtImportService;    
    
    /**
    * 成功:
    * 第四种注入方式: Resource 引用名为需注入bean的name名字
    * 成功注入:XlsImportServiceSon实现;
    * 所以:当使用Resource时,如果Resource(name)中省略name,将会以应用名
    *  “xlsImportServiceSon”作为bean的名字进行查找注入;
    */
    @Resource
    private ImportService xlsImportServiceSon; 
    
    /**
    * 成功:
    * 第五种注入方式: Resource(name="xlsImportService")
    * 成功注入:XlsImportService实现;
    * 所以:当使用Resource(name)时,将会以实例名为
    *  “xlsImportService”作为bean的名字进行查找注入;与引用的名字无关
    */
    @Resource
    private ImportService xlsImportServiceSon; 

    public void componentMethod(){
	xlsImportServiceSon.importInfo();
    }
}


总结:

(1)通常情况下@Autowired是通过byType的方法注入的,当有多个实现类的时候,byType的方式不再是唯一,而需要通过byName的方式来注入,而这个name默认就是根据变量名(应用名)来的。

(2)当一个接口有多个实现类时,通过 @Qualifier("beanName") 注解来指明使用哪一个实现类,实际上也是通过byName的方式实现。

(3)@Autowired(required=false)自动注入,如果找不到不报异常;

(4)当我们使用@Qualifier("beanName")或者@Resource("beanName)时,spring或按这个beanName进行对位查找,与接口后面的引用名字没有任何关系;

  • 注解《2》@Resource

@Resource(name="")
@Resource(type="")
@Resource
@Qualifier("");
默认 autowired by field name
如果 autowired by field name失败,会退化为 autowired by type
可以 通过@Qualifier 显式指定 autowired by qualifier name
如果 autowired by qualifier name失败,会退化为 autowired by field name。
但是这时候如果 autowired by field name失败,就不会再退化为autowired by type了。

@Autowired @Resource 和 @Inject 三个注解的区别:

(1)@Autowired是Spring提供的注解,其他几个都是JDK本身内建的注解,Spring对这些注解也进行了支持;

(2)@Autowired有个required属性,可以配置为false,这种情况下如果没有找到对应的bean是不会抛异常的。@Inject和 @Resource没有提供对应的配置,所以必须找到否则会抛异常。

(3)@Autowired和@Inject基本是一样的,因为两者都是使用AutowiredAnnotationBeanPostProcessor来处理依赖注入。但是@Resource是个例外,它使用的是CommonAnnotationBeanPostProcessor来处理依赖注入

  • 注解《3》@Controller @Service @Repository @Component

上面四个注解分别对应于 控制层,业务层,数据访问层 和 语义不明确的Bean的声明;

常见声明格式:

@Service
//不声明value则,将类名首字母小写作为Bean的名字默认:txtImportService
@Service(value="txtImportService")
@Service("importService")
//使用value作为bean的名字,两种写法
public class TxtImportService implements ImportService {
    @Override
    public void importInfo() {
	System.out.println("TxtImportService");
    }
}
  • 注解《4》事务中用到的注解:

推荐博文:Spring事务配置,声明式事物@Transactional

开启事物注释的控制支持 <tx:annotation-driven>

<!-- 开启事务控制的注解支持 -->
<tx:annotation-driven transaction-manager="transactionManager"/>

<!-- 配置spring的PlatformTransactionManager,名字为默认值 -->
<bean id="transactionManager"      
    class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
	<property name="dataSource" ref="dataSource" />
</bean>

事务注解:@Transactional

@Transactional 可以作用于接口、接口方法、类以及类方法上。当作用于类上时,该类的所有 public 方法 将都具有该类型的事务属性,同时,我们也可以在方法级别使用该标注来覆盖类级别的定义。

(1)虽然 @Transactional 注解可以作用于接口、接口方法、类以及类方法上,但是 Spring 建议不要在接口或者接口方法上使用该注解因为这只有在使用基于接口的代理时它才会生效

(2)另外, @Transactional 注解应该只被应用到 public 方法上,这是由 Spring AOP 的本质决定的。如果你在 protected、private 或者默认可见性的方法上使用 @Transactional 注解,这将被忽略,也不会抛出任何异常。

(3)默认情况下,只有来自外部的方法调用才会被AOP代理捕获,也就是,类内部方法调用本类内部的其他方法并不会引起事务行为,即使被调用方法使用@Transactional注解进行修饰。

事务@Transactional的属性

(1)事务隔离级别:

隔离级别是指若干个并发的事务之间的隔离程度。

MYSQL的默认隔离级别为可重复读:REPEATABLE-READ;

ORACLE的默认隔离级别为读已提交:READ COMMITTED;

SQL SERVER的默认隔离级别为读已提交:READ COMMITTED;

TransactionDefinition 接口中定义了五个表示隔离级别的常量:

TransactionDefinition.ISOLATION_DEFAULT;

TransactionDefinition.ISOLATION_READ_COMMITTED;

TransactionDefinition.ISOLATION_READ_UNCOMMITTED;

TransactionDefinition.ISOLATION_REPEATABLE_READ;

TransactionDefinition.ISOLATION_SERIALIZABLE。

对应的@Transactional的属性为isolation :

@Transactional(isolation=Isolation.REPEATABLE_READ)

(2)事物的传播行为

所谓事务的传播行为是指,如果在 开始 当前事务之前,一个事务上下文已经存在,此时有若干选项可以指定一个事务性方法的执行行为。在TransactionDefinition定义中包括了如下几个表示传播行为的常量:

TransactionDefinition.PROPAGATION_REQUIRED:如果当前存在事务,则加入该事务;如果当前没有事务,则创建一个新的事务。这是默认值。
TransactionDefinition.PROPAGATION_REQUIRES_NEW:创建一个新的事务,如果当前存在事务,则把当前事务挂起。
TransactionDefinition.PROPAGATION_SUPPORTS:如果当前存在事务,则加入该事务;如果当前没有事务,则以非事务的方式继续运行。
TransactionDefinition.PROPAGATION_NOT_SUPPORTED:以非事务方式运行,如果当前存在事务,则把当前事务挂起。 TransactionDefinition.PROPAGATION_NEVER:以非事务方式运行,如果当前存在事务,则抛出异常。
TransactionDefinition.PROPAGATION_MANDATORY:如果当前存在事务,则加入该事务;如果当前没有事务,则抛出异常。
TransactionDefinition.PROPAGATION_NESTED:如果当前存在事务,则创建一个事务作为当前事务的嵌套事务来运行;如果当前没有事务,则该取值等价于TransactionDefinition.PROPAGATION_REQUIRED。
对应的@Transactional的属性为propagation

@Transactional(propagation=Propagation.REQUIRED)

(3)事务超时时间

所谓事务超时,就是指一个事务所允许执行的最长时间,如果超过该时间限制但事务还没有完成,则自动回滚事务。在 TransactionDefinition 中以 int 的值来表示超时时间,其单位是秒。

 默认设置为底层事务系统的超时值,如果底层数据库事务系统没有设置超时值,那么就是none,没有超时限制。

对应的@Transactional的属性为timeout

@Transactional(timeout=10)

(4)事务只读属性

只读事务用于客户代码只读但不修改数据的情形,只读事务用于特定情景下的优化,默认为读写事务。
“只读事务”并不是一个强制选项,它只是一个“暗示”,提示数据库驱动程序和数据库系统,这个事务并不包含更改数据的操作,那么JDBC驱动程序和数据库就有可能根据这种情况对该事务进行一些特定的优化,比方说不安排相应的数据库锁,以减轻事务对数据库的压力。
对应的@Transactional的属性为readOlny

@Transactional(readOnly=true)

(5)事务的回归

默认配置下,spring只有在抛出的异常为运行时 unchecked异常 时才回滚该事务,也就是抛出的异常为RuntimeException的子类(Errors也会导致事务回滚),而抛出checked异常则不会导致事务回滚。可以明确的配置在抛出那些异常时回滚事务,包括checked异常。也可以明确定义那些异常抛出时不回滚事务。
对应的@Transactional的属性为rollbackFor noRollbackFor

@Transactional(rollbackFor=NullPointerException.class)
  • 注解《4》Spring MVC的注解

(1)@RequestMapping 及常用属性说明

@RequestMapping即可以注解在类上也可以注解到具体的方法上;

  • value属性:value支持统配符以及ANT风格的路径,value={"qian",page*}
@RequestMapping("qian")
@RequestMapping(value="/qian")
@RequestMapping(value = {  
        "",  
        "/page",  
        "page*",  
        "view/*,**/msg"  
 })  
  • param 和  headers 属性:用于限定请求中和请求的消息头中是否含有指定的参数
//请求中必须包含stu参数;
@RequestMapping(value="/handle",params="stu")
//请求中必须包含username参数,age参数,并且age的值为20,参数中不能包含pwd参数
@RequestMapping(value="rq1",params={"username","age=20","!pwd"})

@RequestMapping(value="rq1", headers = "headers = "Content-Type=application/json")
  • produces 和 consumes 属性:分别用于指定返回的内容类型和处理请求提交的内容的类型;

推荐博文:Head中Context-Type讲解

// 返回json数据的字符编码为utf-8,下边的代码可以省略produces属性,因为
// 我们已经使用了注解@responseBody就是返回值是json数据.
@Controller 
@RequestMapping(value = "/pets/{petId}", 
                produces="MediaType.APPLICATION_JSON_VALUE;charset=utf-8") 
@ResponseBody 
public Pet getPet(@PathVariable String petId, Model model) { 
    // implementation omitted 
} 
//方法仅处理request Content-Type为“application/json”类型的请求
@Controller 
@RequestMapping(value = "/pets", consumes="application/json") 
public void addPet(@RequestBody Pet pet, Model model) { 
    // implementation omitted 
} 

(2)@RequestBody 和 @ResponseBody

@RequestBody

用于读取Request请求的body部分的数据,使用系统默认配置的HttpMessageConverter进行解析,然后把相应的数据绑定到解析方法要返回的对象上; 再把HttpMessageConverter返回的对象数据绑定到 controller中方法的参数上。

@ResponseBody

该注解的作用是将controller的方法返回的对象通过适当的转换器转换为指定的格式之后,写入到response对象的body区,通常用来返回JSON数据或者是XML数据,需要注意的呢,在使用此注解之后不会再走 试图处理器,而是直接将数据写入到输入流中,他的效果等同于通过response对象输出指定格式的数据。(也就是说 在使用@RequestMapping后,返回值通常解析为跳转路径。加上@responsebody后,返回结果直接写入HTTP response body中,不会被解析为跳转路径。比如异步请求,希望响应的结果是json数据,那么加上@responsebody 后,就会直接返回json数据。

(1)Spring mvc 处理json请求时,默认使用的是 MappingJackson2HttpMessageConverter ;

<!--引入jackson包-->
<dependency>
   <groupId>com.fasterxml.jackson.core</groupId>
   <artifactId>jackson-core</artifactId>
   <version>2.7.0</version>
</dependency>
<dependency>
   <groupId>com.fasterxml.jackson.core</groupId>
   <artifactId>jackson-databind</artifactId>
   <version>2.7.0</version>
 </dependency>
 <dependency>
   <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-annotations</artifactId>
    <version>2.7.0</version>
 </dependency>

(2)当返回json数据时,防止老版IE出现下载界面;

<mvc:annotation-driven>
        <mvc:message-converters>
            <ref bean="stringHttpMessageConverter"/>
            <ref bean="mappingJackson2HttpMessageConverter"/>
        </mvc:message-converters>
</mvc:annotation-driven>

<bean id="stringHttpMessageConverter"
      class="org.springframework.http.converter.StringHttpMessageConverter"/>

<!--解决IE浏览器json文件下载和json数据中文乱码的问题-->
<bean id="mappingJackson2HttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
        <property name="supportedMediaTypes">
            <list>
                <value>text/html;charset=UTF-8</value>
            </list>
        </property>
</bean>
// post->String字符串数组
function sendStrArr() {
	var strArr = new Array("Saab","Volvo","BMW");
	var str = "userNamewangwang,age:30";
	$.ajax({
		type:"POST",
		url:"qian/strArrData",
		dataType:"json",
		contentType:"application/json;charset=utf-8",
		data:JSON.stringify(strArr),
		success:function(data){
			
		}
	});
}

@RequestMapping(value="strArrData")
public void receiveString(@RequestBody List<String> strs){
	System.out.println(strs+ ""+strs.size());
}

/*************** post -> java bean *********************/
function userData() {
	var user = {"userName":"zhangsan","age":"20"};
	$.ajax({
		type:"POST",
		url:"qian/userData",
		dataType:"json",
		contentType:"application/json;charset=utf-8",
		data:JSON.stringify(user),
		success:function(data){
			alert(data.age);
		}
	});
}

@RequestMapping(value="userData")
@ResponseBody
public User receiveStringArr(@RequestBody User user){
	System.out.println(user.toString());
	return user;
}

/**************** post -> List<bean> *************************/
function usersData() {
	var userArr = [];
	var user1 = {"userName":"zhangsan","age":"20"};
	var user2 = {"userName":"wangwang","age":"30"};
	userArr.push(user1);
	userArr.push(user2);
	$.ajax({
		type:"POST",
		url:"qian/usersData",
		dataType:"json",
		contentType:"application/json;charset=utf-8",
		data:JSON.stringify(userArr),
		success:function(data){
			alert(data[0].age);
		}
	});
}

@RequestMapping(value="usersData", method=RequestMethod.POST)
@ResponseBody
public List<User> saveUser(@RequestBody List<User> users){
	for(User u :users){
		System.out.println(u.toString());
	}
	return users;
}

关于@RequsetBody可以参考下面三个博文

>> SpringMVC json(1)

>> SpringMVC json(2)

>> SpringMVC json(3)

(3)@PathVariable

将url中相应的部分直接映射到方法对应于@PathVariable注解的变量上;

注意:

(1)spring3 以后,@PathVariable不仅支持Get方法传值,还支持Post方法传值;

(2)对于请求路径的结尾中带有小数点(.)时,Spring会去掉最后一个(.)后面的内容,

GET: http://host:port/program/viewFile/module/201612201231445.pdf

@RequestMapping(value="viewFile/{module}/{filename}",method=RequestMethod.GET)
@RequestMapping(value="viewFile/{module}/{filename:.+}",method=RequestMethod.GET)
public void viewFile(HttpSession session,HttpServletResponse response,@PathVariable String module, @PathVariable String filename){
     //解析后获得到的文件名称为201612201231445并没有或追文件后缀 
}

(3)防止出现空值的问题

(4)@RequestParam

主要用于在SpringMVC后台控制层获取参数,等价操作:request.getParameter("userName")  等价于 @RequestParam(value="userName");

name / value:参数名字,即入参的请求参数名字,如username表示请求的参数区中的名字为username的参数的值将传入;

required:是否必须,默认是true,表示请求中一定要有相应的参数,否则将报404错误码;

defaultValue:默认值,表示如果请求中没有同名参数时的默认值。

具体用户参考一篇不错的文章:@RequestParam的具体使用

 

<form action="/gadget/testRequestParam" method="post">    
    参数inputStr:<input type="text" name="inputStr"> test
    参数intputInt:<input type="text" name="inputInt"> 123
</form>

@RequestMapping("testRequestParam")    
public String filesUpload(@RequestParam String inputStr, HttpServletRequest request){
    System.out.println(inputStr);     
    int inputInt = Integer.valueOf(request.getParameter("inputInt"));  
    System.out.println(inputInt);   
}
output: test
        123

如果用@RequestParam注解的参数是int基本类型,但是required=false,这时如果不传参数值会报错,因为不传值,会赋值为null给int,这个不可以 建议使用包装类型代替基本类型,如使用“Integer”代替“int

@RequestParam VS @RequestBody

@RequestParam 

(1) 常用来处理简单类型的绑定,通过Request.getParameter() 获取的String可直接转换为简单类型的情况( String--> 简单类型的转换操作由ConversionService配置的转换器来完成);因为使用request.getParameter()方式获取参数,所以可以处理get 方式中queryString的值,也可以处理post方式中 body data的值;

(2)用来处理Content-Type: 为 application/x-www-form-urlencoded编码的内容,提交方式GET、POST;

@RequestBody

(1)该注解常用来处理Content-Type: 不是application/x-www-form-urlencoded编码的内容,例如application/json, application/xml等;它是通过使用HandlerAdapter 配置的HttpMessageConverters来解析post data body,然后绑定到相应的bean上的。

(2)因为配置有FormHttpMessageConverter,所以也可以用来处理 application/x-www-form-urlencoded的内容,处理完的结果放在一个MultiValueMap<String, String>里

(5)@ModelAttribute

(1)ModelAttribute标注在方法上,在调用目标处理方法前,会先逐个调用在方法级上标注了@ModelAttribute 的方法;注意:使用 @ModelAttribute(“attributeName”) 标记方法的时候,若未指定atrributeName,则使用返回类型的类名称(首字母小写)作为属性名称;

(2)ModelAttribute标注在方法的参数上,

//UserController.java在方法入参处使用@ModelAttribute
@RequestMapping(value="/model")
public String handleModel(@ModelAttribute("user") User user){
    user.setUserId("1000");
    return "/user/createSuccess";
}

SpringMVC 将请求消息绑定到User对象中,然后再以“user”为键将User对象放到模型中。对于JSP视图来说,SpringMVC会将模型数据转储到ServletRequest的属性列表中(即通过ServletRequest#setAttribute(String name,Object o )保存)。handleModel()返回的逻辑视图名为 /user/createSuccess,对应createSuccess.jsp视图对象,这样createSuccess.jsp就可以使用${user.userName}等方式顺利访问到模型中的数据了。

SpringMVC中@ModelAttribute与@RequestBody的区别:参考

(6)SessionAttribute

推荐一个不错的博文说的很详细:SpringMVC @SessionAttributes 使用详解以及源码分析

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
package com.org.core.entity; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; @Entity public class User { @Id @GeneratedValue private int id; @Column(name = "username") private String username; @Column(name = "password") private String password; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } } package com.org.core.service.impl; import java.util.List; import javax.annotation.Resource; import org.springframework.stereotype.Service; import com.org.core.dao.UserDao; import com.org.core.entity.User; import com.org.core.service.UserService; /** *@Author:liangjilong *@Date:2014-2-25 *@Version:1.0 *@Description: */ @Service public class UserServiceImpl implements UserService{ @Resource private UserDao userDao; @Override public List<User> getListUsers() { return userDao.getListUsers(); } } package com.org.core.dao.impl; import java.util.List; import javax.annotation.Resource; import org.springframework.orm.hibernate3.HibernateTemplate; import org.springframework.stereotype.Repository; import com.org.core.dao.UserDao; import com.org.core.entity.User; /** *@Author:liangjilong *@Date:2014-2-25 *@Version:1.0 *@Description: */ @Repository public class UserDaoImpl implements UserDao { @Resource private HibernateTemplate hibernateTemplate; @SuppressWarnings("unchecked") public List<User> getListUsers() { String hql="From User"; List<User> lists=hibernateTemplate.find(hql); return lists; } } package com.org.core.action; import java.util.List; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; import com.org.core.entity.User; import com.org.core.service.UserService; import com.org.utils.servlet.ServletUtils; /** *@Author:liangjilong *@Date:2014-2-25 *@Version:1.0 *@Description: */ @Controller public class UserController{ @Resource private UserService userService; @RequestMapping(value="/userList1.do") public String geUserList1(HttpServletRequest request ,HttpServletResponse response) throws Exception { List<User> lists=userService.getListUsers(); if(lists!=null){ //request.setAttribute("userList", lists); ServletUtils.setRequestValue("userList", lists); } return "/user/userList";//user文件下的userList.jsp } @RequestMapping(value="/userList2.do") public ModelAndView geUserList2(HttpServletRequest request ,HttpServletResponse response) throws Exception { List<User> lists=userService.getListUsers(); if(lists!=null){ //request.setAttribute("userList", lists); ServletUtils.setRequestValue("userList", lists); } return new ModelAndView("/user/userList"); } } <?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:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd" default-lazy-init="true"> <context:annotation-config/> <!-- 扫描包 --> <context:component-scan base-package="com.org.core"/> <bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> <property name="prefix" value="/jsp/" /> <property name="suffix" value=".jsp" /> </bean> <!-- 配置jdbc --> <bean class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer"> <property name="locations"> <value>classpath:jdbc.properties</value> </property> </bean> <!-- 配置數據源 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${jdbc.driver}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <!-- 连接池启动时的初始值 --> <property name="initialSize" value="1"/> <property name="maxActive" value="500"/> <property name="maxIdle" value="2"/> <property name="minIdle" value="1"/> </bean> <!-- 配置sessionFactory 注解配置 org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean 配置形式: org.springframework.orm.hibernate3.LocalSessionFactoryBean --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="packagesToScan"> <list> <value>com.org.core.entity</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">${hibernate.dialect}</prop> <prop key="hibernate.show_sql">true</prop> </props> </property> </bean> <!-- 配置hibernateTemplate --> <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <!-- Spring AOP config配置切点 --> <aop:config> <aop:pointcut expression="execution(public * com.org.core.service.*.*(..))" id="bussinessService" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="bussinessService" /> </aop:config> <!-- 配置那个类那个方法用到事务处理 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="get*" read-only="true" /> <tx:method name="add*" propagation="REQUIRED" /> <tx:method name="update*" propagation="REQUIRED" /> <tx:method name="delete*" propagation="REQUIRED" /> <tx:method name="*" propagation="REQUIRED" /> </tx:attributes> </tx:advice> </beans>
<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:oscache="http://www.springmodules.org/schema/oscache" 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 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springmodules.org/schema/oscache http://www.springmodules.org/schema/cache/springmodules-oscache.xsd"> <context:annotation-config/> <!-- 扫描包 --> <context:component-scan base-package="com.org"/> <bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> <!-- 匹配jsp文件下面的所有.jsp的页面 --> <property name="prefix" value="/jsp/" /> <property name="suffix" value=".jsp" /> </bean> <!-- <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/" p:suffix=".jsp" />--> <!-- 配置jdbc --> <bean class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer"> <property name="locations"> <value>classpath:jdbc.properties</value> </property> </bean> <!-- 配置數據源 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${jdbc.driver}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <!-- 连接池启动时的初始值 --> <property name="initialSize" value="1"/> <property name="maxActive" value="500"/> <property name="maxIdle" value="2"/> <property name="minIdle" value="1"/> </bean> <!-- 定义Ibatis配置 org.springframework.orm.ibatis.SqlMapClientFactoryBean --> <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean"> <property name="configLocation"> <value>classpath:ibatis-Base.xml</value> </property> <property name="dataSource"> <ref bean="dataSource"/> </property> </bean> <!-- 配置sqlMapClientTemplate模板 --> <bean id="sqlMapClientTemplate" class="org.springframework.orm.ibatis.SqlMapClientTemplate"> <property name="sqlMapClient" ref="sqlMapClient"/> </bean> <!-- 配置 transactionManager事物管理--> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <!-- Spring AOP config配置切点 --> <aop:config> <aop:pointcut expression="execution(* com.org.service.*.*(..))" id="bussinessService" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="bussinessService"/> </aop:config> <!-- 配置那个类那个方法用到事务处理 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="get*" read-only="true"/> <tx:method name="add*" propagation="REQUIRED"/> <tx:method name="update*" propagation="REQUIRED"/> <tx:method name="delete*" propagation="REQUIRED"/> <tx:method name="*" propagation="REQUIRED"/> </tx:attributes> </tx:advice> </beans>

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值