您的位置:首页 > 编程语言 > Java开发

spring常用的一些注解以及注解注入总结(二)

2017-07-20 19:33 411 查看
@ModelAttribute

• 作用域:request

• 例如

@RequestMapping(“/base/userManageCooper/init.do”)

public String handleInit(@ModelAttribute(“queryBean”) ManagedUser sUser,Model model,){

• 或者

@ModelAttribute(“coopMap”)// 将coopMap 返回到页 面

public Map

package cn.outofmemory.helloannotation;

import org.springframework.stereotype.Repository;

@Repository
public class CarDao {

public void insertCar(String car) {
String insertMsg = String.format("inserting car %s", car);
System.out.println(insertMsg);
}

}


新建CarService类,并给该类标注@Service注解,在这个类中定义CarDao的字段,并通过Autowired来修饰此字段,这样上面定义的CarDao类的实例就会自动注入到CarService的实例中了。

package cn.outofmemory.helloannotation;

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

@Service
public class CarService {

@Autowired
private CarDao carDao;

public void addCar(String car) {
this.carDao.insertCar(car);
}
}


注意:Autowired注解有一个可以为空的属性required,可以用来指定字段是否是必须的,如果是必需的,则在找不到合适的实例注入时会抛出异常。

下面我们在App.Java中使用上面测试下注解注入:

package cn.outofmemory.helloannotation;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args )
{
ApplicationContext appContext = new AnnotationConfigApplicationContext("cn.outofmemory.helloannotation");
CarService service = appContext.getBean(CarService.class);
service.addCar("宝马");
}
}


在上面的main方法中首先我们初始化了appContext,他是AnnotationConfigApplicationContext,它的构造函数接受一个package的名称,来限定要扫描的package。然后就可以通过appContext的getBean方法获得CarService的实例了。

上面的例子非常简单,单纯的使用AnnotationConfigApplicationContext就可以了,但是在实际项目中情况往往没有这么简单,还是需要spring配置文件的。在spring配置文件中也可以通过下面的配置让spring自动扫描注解配置。

<!-- bean annotation driven -->
<context:annotation-config />
<context:component-scan base-package="cn.outofmemory.helloannotation" >
</context:component-scan>


下面我们看下混合使用spring配置和注解的例子,首先在项目中添加source folder,src/main/resources,并添加spring.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd ">
<!-- bean annotation driven --> <context:annotation-config /> <context:component-scan base-package="cn.outofmemory.helloannotation" > </context:component-scan>

<bean id="sqliteCarDao" class="cn.outofmemory.helloannotation.CarDao" >
<constructor-arg name="driver" value="sqlite"/>
</bean>
</beans>


在上面的配置文件中,我们通过context:annotation-config和context:component-sacn节点来指定要扫描注解注入,然后又定义了一个id为sqliteCarDao的bean,它的构造函数的driver值为sqlite。

我们修改下App.java使用xml配置文件,再运行下App看下会怎样。

package cn.outofmemory.helloannotation;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args )
{
//ApplicationContext appContext = new AnnotationConfigApplicationContext("cn.outofmemory.helloannotation");

ApplicationContext appContext = new ClassPathXmlApplicationContext("/spring.xml");
CarService service = appContext.getBean(CarService.class);
service.addCar("宝马");
}
}


运行程序发现输出为:inserting car 宝马 into MySQL,显然CarService自动注入的CarDao使用了默认构造函数构造的实例。是否可以通过注解指定使用spring.xml中配置的sqliteCarDao呢?

我们可以修改下CarService类,通过Qualifier注解来指定要使用的bean的名字。

如下,在指定Autowired注解时,同时指定Qualifier注解指定bean的名字

@Autowired
@Qualifier("sqliteCarDao")
private CarDao carDao;


重新运行下App.java 这次输出的是inserting car 宝马 into sqlite,这次使用了spring.xml中配置的bean了。

在文中开头我们还提到了Resouce注解,这个注解可以指定名字注入,我们再次修改下CarService类:

@Resource(name="sqliteCarDao")
private CarDao carDao;


javax.annotation.Resource注解实现的效果和@Autowired+@Qualifier的效果是一样的。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: