上面的Autowired和Resource是用来修饰字段,构造函数,或者设置方法,并做注入的。而Service,Controller,Repository,Component则是用来修饰类,标记这些类要生成bean。
maven的pom.xml
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <org.springframework.version>3.1.1.RELEASE</org.springframework.version> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${org.springframework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>${org.springframework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${org.springframework.version}</version> <type>jar</type> <scope>test</scope> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.2</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.2.3</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.2.3</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.2.3</version> </dependency> </dependencies>
1、建立dao层,给它添加@Repository注解(注:dao类不需实现某个接口,当然实现接口也可以)
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); } }
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); } }
package cn.outofmemory.helloannotation; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class App { public static void main( String[] args ){ ApplicationContext appContext = new AnnotationConfigApplicationContext("cn.outofmemory.helloannotation"); CarService service = appContext.getBean(CarService.class); service.addCar("宝马"); } }
4、applicationContext.xml配置文件(把该文件放到classpath下)
<?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> </beans>
package cn.outofmemory.helloannotation; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class App { public static void main( String[] args ) { //ApplicationContext appContext = new AnnotationConfigApplicationContext("cn.outofmemory.helloannotation"); <span style="white-space:pre"> String[] fileUrl = new String[]{"classpath*:applicationContext.xml"}; </span> ApplicationContext appContext = new ClassPathXmlApplicationContext(fileUrl)<span style="font-family: 'Comic Sans MS';">;</span> CarService service = appContext.getBean(CarService.class); service.addCar("宝马"); } }
CarService service = (CarService)appContext.getBean("carService");名字的规范为 驼峰
参考:http://outofmemory.cn/code-snippet/3670/spring-inject-by-annotation
版权声明:本文为博主原创文章,未经博主允许不得转载。
Spring依赖注入——java项目中使用spring注解方式进行注入
原文地址:http://blog.csdn.net/liuxiao723846/article/details/47172015