如何在Maven中配置Springmaven多项目依赖配置

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
您的访问请求被拒绝 403 Forbidden - ITeye技术社区
您的访问请求被拒绝
亲爱的会员,您的IP地址所在网段被ITeye拒绝服务,这可能是以下两种情况导致:
一、您所在的网段内有网络爬虫大量抓取ITeye网页,为保证其他人流畅的访问ITeye,该网段被ITeye拒绝
二、您通过某个代理服务器访问ITeye网站,该代理服务器被网络爬虫利用,大量抓取ITeye网页
请您点击按钮解除封锁&下次自动登录
现在的位置:
& 综合 & 正文
使用Maven配置spring
使用Maven使用spring(注解版)
1、 创建maven项目
NewàProjectàMaven Projectà不勾选 Create a simple project
à选择maven-archetype-quickstart
à填写Group ID,artifact id
至此。Maven项目创建完成,目录结果如下。
2、 加入spring 依赖
打开pom.xml文件,最后一个标签
&dependency&
&groupId&org.springframework&/groupId&
&artifactId&spring-context&/artifactId&
&version&<span style="color:#.1.1.RELEASE&/version&
&/dependency&
找到maven setting.xml(maven
.m2目录下),
&repository&
&id&springsource-repo&/id&
&name&SpringSource Repository&/name&
&url&http://repo.springsource.org/release&/url&
&/repository&
具体配置可以参考
说明:关于为什么要配置spring-context 网站上没有给出明确说明,只有下面一段看不懂的话。我的理解是只要用基础的spring引这个就行了。
spring-context.jar   这个jar文件为Spring核心提供了大量扩展。可以找到使用Spring ApplicationContext特性时所需的全部类,JDNI所需的全部类,UI方面的用来与模板(Templating)引擎如 Velocity、FreeMarker、JasperReports集成的类,以及校验Validation方面的相关类。
3、 写spring配置文件和类
新建sourse folder 名字 src/main/resources
新建xml文件
目录结构:
编辑applicationContext.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"
xmlns:util="http://www.springframework.org/schema/util"
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-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd"&
************以上代码可以在spring的例子中找到***********
加入Person类
package com.syz.test<span style="color:#;
public class Person {
public String name;
public String age;
public String getName() {
return name;
public void setName(String name) {
this.name =
public String getAge() {
return age;
public void setAge(String age) {
this.age =
package com.syz.test<span style="color:#;
* Hello world!
public class App
public Person person;
public Person getPerson() {
return person;
public void setPerson(Person person) {
this.person =
public static void main( String[] args )
System.out.println( "Hello World!" );
配置好的applicationContext.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"
xmlns:util="http://www.springframework.org/schema/util"
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-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd"&
&bean id="person" class="com.syz.test01.Person"&
&property name="name" value="zhangsan"&&/property&
&property name="age" value="12"&&/property&
&bean id="app" class="com.syz.test01.App"&
&property name="person" ref="person"&&/property&
4、 编写测试代码
AppTest.java 代码
package com.erik.spring.
import junit.framework.T
import junit.framework.TestC
import junit.framework.TestS
import org.springframework.beans.factory.BeanF
import org.springframework.context.support.ClassPathXmlApplicationC
* Unit test for simple App.
public class AppTest
extends TestCase
* Create the test case
* @param testName name of the test case
public AppTest( String testName )
super( testName );
* @return the suite of tests being tested
public static Test suite()
return new TestSuite( AppTest.class );
* Rigourous Test :-)
public void testApp()
BeanFactory bf = new ClassPathXmlApplicationContext(
"applicationContext.xml");
App at = (App) bf.getBean("app");
System.out.println(at.getPerson().getName());
System.out.println(at.getPerson().getAge());
assertTrue( true );
用junit运行该测试类
具体使用参考下一篇:
&&&&推荐文章:
【上篇】【下篇】Spring依赖注入:注解注入总结 - 为程序员服务
为程序员服务
Spring依赖注入:注解注入总结
注解注入顾名思义就是通过注解来实现注入,Spring和注入相关的常见注解有Autowired、Resource、Qualifier、Service、Controller、Repository、Component。
Autowired是自动注入,自动从spring的上下文找到合适的bean来注入
Resource用来指定名称注入
Qualifier和Autowired配合使用,指定bean的名称
Service,Controller,Repository分别标记类是Service层类,Controller层类,数据存储层的类,spring扫描注解配置时,会标记这些类要生成bean。
Component是一种泛指,标记类是组件,spring扫描注解配置时,会标记这些类要生成bean。
上面的Autowired和Resource是用来修饰字段,构造函数,或者设置方法,并做注入的。而Service,Controller,Repository,Component则是用来修饰类,标记这些类要生成bean。
下面我们通过实例项目来看下spring注解注入的使用。
首先新建一个maven项目,并在pom中添加spring相关的依赖,如果不知道添加那些依赖,请参照上一篇文章。
然后新建CarDao类,给它添加@Repository注解,如下代码:
package cn.outofmemory.
import org.springframework.stereotype.R
@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.
import org.springframework.beans.factory.annotation.A
import org.springframework.stereotype.S
public class CarService {
@Autowired
private CarDao carD
public void addCar(String car) {
this.carDao.insertCar(car);
注意:Autowired注解有一个可以为空的属性required,可以用来指定字段是否是必须的,如果是必需的,则在找不到合适的实例注入时会抛出异常。
下面我们在App.java中使用上面测试下注解注入:
package cn.outofmemory.
import org.springframework.context.ApplicationC
import org.springframework.context.annotation.AnnotationConfigApplicationC
* 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&/&
在上面的配置文件中,我们通过context:annotation-config和context:component-sacn节点来指定要扫描注解注入,然后又定义了一个id为sqliteCarDao的bean,它的构造函数的driver值为sqlite。
我们修改下App.java使用xml配置文件,再运行下App看下会怎样。
package cn.outofmemory.
import org.springframework.context.ApplicationC
import org.springframework.context.annotation.AnnotationConfigApplicationC
import org.springframework.context.support.ClassPathXmlApplicationC
* 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 carD
重新运行下App.java 这次输出的是inserting car 宝马 into sqlite,这次使用了spring.xml中配置的bean了。
在文中开头我们还提到了Resouce注解,这个注解可以指定名字注入,我们再次修改下CarService类:
@Resource(name=&sqliteCarDao&)
private CarDao carD
javax.annotation.Resource注解实现的效果和@Autowired+@Qualifier的效果是一样的。
您可能的代码
相关聚客文章
荣誉:2088
相关专栏文章}

我要回帖

更多关于 spring依赖注入配置 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信