springmvc注解配置

<?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.eric.spring”>
</beans>

1.annotation-config是对标记了 Spring's @Required、@Autowired、JSR250's @PostConstruct、@PreDestroy、@Resource、JAX-WS's @WebServiceRef、EJB3's @EJB、JPA's @PersistenceContext、@PersistenceUnit等注解的类进行对应的操作使注解生效。
2.base-package为需要扫描的包(含所有子包),负责扫描那些类有注解。

注:前面讲要使用注解需要配置: <context:annotation-config />但如果使用了@Component就不需要加它了,因为:<context:component-scan base-package="com.zchen">里面默认了<context:annotation-config />。

而@Controller, @Service, @Repository是@Component的细化,这三个注解比@Component带有更多的语义,它们分别对应了控制层、服务层、持久层的类。

@Component泛指组件,当组件不好归类的时候我们可以使用这个注解进行标注,(现在可以都用此注解,可以只使用单一组件)

事例

1.定义接口PersonDao

package com.qn.dao;

public interface PersonDao {
     void add();
}

2.定义接口PersonService

package com.qn.dao;

import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

public interface PersonService {
 void save();

}

3.定义类PersonDaoBean实现PersonDao

package com.qn.service.impl;

import com.qn.dao.PersonDao;

public class PersonDaoBean implements PersonDao{

public void add() {
  System.out.println("PersonDaoBean的添加方法");
 }

}

4.定义类PersonServiceBean实现PersonService

package com.qn.service.impl;

import javax.annotation.Resource;

import com.qn.dao.PersonDao;
import com.qn.dao.PersonService;

public class PersonServiceBean implements PersonService {
    @Resource
    private PersonDao personDao;
    private String name;
    public PersonServiceBean(){}
 public PersonServiceBean(PersonDao personDao, String name) {
  this.personDao = personDao;
  this.name = name;
 }

public void save(){
  personDao.add();
  System.out.println(name);
 }
}

5.在bean中进行配置

<?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/contexthttp://www.springframework.org/schema/context/spring-context-2.5.xsd">
          <context:annotation-config/>
          <bean name="personDao" class="com.qn.service.impl.PersonDaoBean"></bean>
          <bean name="personService" class="com.qn.service.impl.PersonServiceBean"></bean>
</beans>

6.在test中进行测试

package com.qn.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.qn.dao.PersonService;

public class test {

public static void main(String []args){
  AbstractApplicationContext ctx=new ClassPathXmlApplicationContext("beans.xml");
  PersonService personService=(PersonService) ctx.getBean("personService");
  personService.save();  
 }
}

@Resource

的作用相当于@Autowired,只不过@Autowired按byType自动注入,而@Resource默认按 byName自动注入罢了。@Resource有两个属性是比较重要的,分是name和type,Spring将@Resource注解的name属性解析为bean的名字,而type属性则解析为bean的类型。所以如果使用name属性,则使用byName的自动注入策略,而使用type属性时则使用byType自动注入策略。如果既不指定name也不指定type属性,这时将通过反射机制使用byName自动注入策略。

  @Resource装配顺序 
  1. 如果同时指定了name和type,则从Spring上下文中找到唯一匹配的bean进行装配,找不到则抛出异常 
  2. 如果指定了name,则从上下文中查找名称(id)匹配的bean进行装配,找不到则抛出异常 
  3. 如果指定了type,则从上下文中找到类型匹配的唯一bean进行装配,找不到或者找到多个,都会抛出异常 
  4. 如果既没有指定name,又没有指定type,则自动按照byName方式进行装配;如果没有匹配,则回退为一个原始类型进行匹配,如果匹配则自动装配

链接:http://www.cnblogs.com/pwc1996/p/4839131.html

http://www.cnblogs.com/loveweiwei/p/3901387.html

上一篇:JAX-WS(一)之使用wsgen从Java创建简单的WebService


下一篇:linux下tar命令解压到指定的目录