标签:style blog http io ar color os sp java
本系列介绍spring的入门,本章讲spring的基本配置与控制反转。
本系列文章,我采用了maven构建java项目,没有采用maven的同样适用。
1. 创建maven项目,我创建的项目名称为springdemo。
2. 添加依赖包,我是通过maven添加的包,maven相关配置如下:
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.0.1.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.0.1.RELEASE</version> </dependency>
当然,也可以直接添加jar文件。
3. 创建简单包结构
我创建了entity 、dao、business等包结构。
4. 编写简单代码
user,空代码,如下:
package com.chzhao.model; public class User { }
接口 IUserDao
package com.chzhao.dao; import com.chzhao.model.User; public interface IUserDao { public User findUserById(); }
接口实现
package com.chzhao.dao; import com.chzhao.model.User; public class UserDaoImpl implements IUserDao { public User findUserById() { System.out.println("dao"); return null; } }
调用
package com.chzhao.springdemo; import com.chzhao.dao.IUserDao; import com.chzhao.model.User; public class UserManager { public IUserDao getDao() { return dao; } public void setDao(IUserDao dao) { this.dao = dao; } private IUserDao dao; public User findUser() { return dao.findUserById(); } }
main初始化
package com.chzhao.springdemo; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class App { public static void main(String[] args) { ApplicationContext act = new ClassPathXmlApplicationContext( "applicationContext.xml"); UserManager um = (UserManager) act.getBean("userManager"); um.findUser(); } }
相应的配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="UserDaoImpl" class="com.chzhao.dao.UserDaoImpl"/> <bean name="userManager" class="com.chzhao.springdemo.UserManager"> <property name="dao" > <ref bean="UserDaoImpl"/> </property> </bean> </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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" /> <bean id="UserDaoImpl" class="com.chzhao.dao.UserDaoImpl"/> <bean name="userManager" class="com.chzhao.springdemo.UserManager"> </bean> </beans>
接口实现
package com.chzhao.dao; import org.springframework.stereotype.Repository; import com.chzhao.model.User; @Repository public class UserDaoImpl implements IUserDao { public User findUserById() { System.out.println("dao"); return null; } }
调用
package com.chzhao.springdemo; import org.springframework.beans.factory.annotation.Autowired; import com.chzhao.dao.IUserDao; import com.chzhao.model.User; public class UserManager { @Autowired private IUserDao dao; public User findUser() { return dao.findUserById(); } }
两种方法实现的是一样的。
标签:style blog http io ar color os sp java
原文地址:http://www.cnblogs.com/wardensky/p/4106958.html