目的:了解spring框架中的注解

前言:同样是使用idea创建一个普通的maven工程(如何创建一个普通的Maven工程可以参考mybatis入门第一天的详解)。

项目结构:

代码编辑:

在项目中引入需要的Maven开发坐标

 <dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.12</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
  • 在demo文件下 编写接口UserService
 package zh.test.deomo;

 public interface UserService {
public void sayHi();
}
  • 实现接口,编写UserServiceImpl
 package zh.test.deomo;

 public class UserServiceImpl implements UserService {

     public void sayHi() {
System.out.println("SayHi");
}
}
  • 载demo1文件下,编写关于spring使用setter的方式注入

  创建一个DeptDao的接口

 package zh.test.deomo1;

 public interface DeptDao {
public void save();
}

  实现该接口,DeptDaoImpl

 package zh.test.deomo1;

 public class DeptDaoImpl implements DeptDao {
public void save() {
System.out.println("持久层。。保存。。。");
}
}

  编写DeptService的接口

 package zh.test.deomo1;

 public interface DeptService {
public void save();
}

实现接口DeptServiceImpl

 package zh.test.deomo1;

 public class DeptServiceImpl implements DeptService {
private DeptDao deptDao; //普通的数据类型,也需要提供set的方法注入
private String name;
private int age; public void setName(String name) {
this.name = name;
} public void setAge(int age) {
this.age = age;
} //依赖注入通过set的方法传递进来
public void setDeptDao(DeptDao deptDao) {
System.out.println("set被调用。。。");
this.deptDao = deptDao;
} public void save() {
System.out.println("业务层。。。保存");
System.out.println(name+":"+age);
deptDao.save();
}
}
  • 在deomo2的文件下编写关于使用构造函数的方式注入Car.java文件
 package zh.test.deomo2;

 public class Car {
private String name;
private Double money; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public Double getMoney() {
return money;
} public void setMoney(Double money) {
this.money = money;
} @Override
public String toString() {
return "Car{" +
"name='" + name + '\'' +
", money=" + money +
'}';
} public Car(String name, Double money) {
this.name = name;
this.money = money;
}
}
  • 在deomo4文件下编写扩展的类,主要是存放集合、数组、属性文件
 package zh.test.deomo4;

 import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Properties; public class Extension {
//数组
private String [] strs; //集合
private List<String> list; //map
private Map<String,String> map; //属性文件
private Properties properties; @Override
public String toString() {
return "Extension{" +
"strs=" + Arrays.toString(strs) +
", list=" + list +
", map=" + map +
", properties=" + properties +
'}';
} public String[] getStrs() {
return strs;
} public void setStrs(String[] strs) {
this.strs = strs;
} public List<String> getList() {
return list;
} public void setList(List<String> list) {
this.list = list;
} public Map<String, String> getMap() {
return map;
} public void setMap(Map<String, String> map) {
this.map = map;
} public Properties getProperties() {
return properties;
} public void setProperties(Properties properties) {
this.properties = properties;
}
}
  • 编写applicationContxt.xml文件在resources目录下
 <?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:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> <!--IOC管理bean-->
<!--配置实现类,配置组建bean管理-->
<!--
标签bean 用来管理类,把类交给IOC容器管理,IOC容器把当前的这个类创建对象,存入到IOC容器中。
id="唯一的值,自定义"
class="管理类的全路径,包名+类名 底层使用反射代码创建对象"
class.forName("zh.test.deomo.UserServiceImpl") scope="是创建之后的对象是单例的还是多例的" 默认是单例的
scope="singleton" 单例的
scope="prototype" 多例的
区别:如果是多例的在程序加载配置文件的时候不会立即创建一个对象,而是什么时候用到什么时候会用到。
如果是单例的则是在程序已加载的时候就会创建一个对象。只要IOC的容器存在那么单例的对象就一直存在。
-->
<bean id="userService" class="zh.test.deomo.UserServiceImpl" /> <!--通过set的方法注入-->
<bean id="deptService" class="zh.test.deomo1.DeptServiceImpl">
<!--依赖注入 name="service中的属性"-->
<property name="deptDao" ref="deptDao"/>
<!--给普通的属性注入值-->
<property name="name" value="白天黑天阴天"/>
<property name="age" value="20"/>
</bean>
<bean id="deptDao" class="zh.test.deomo1.DeptDaoImpl"></bean> <!--属性的构造方法-->
<bean id="car" class="zh.test.deomo2.Car">
<constructor-arg name="name" value="劳斯劳斯"/>
<constructor-arg name="money" value="11111"/>
</bean> <!--扩展方法-->
<bean id="extension" class="zh.test.deomo4.Extension">
<!--数组-->
<property name="strs">
<array>
<value>l</value>
<value>g</value>
</array>
</property> <!--集合-->
<property name="list">
<list>
<value>james</value>
<value>davis</value>
</list>
</property> <!--map-->
<property name="map">
<map>
<entry key="lakers" value="lebron"/>
<entry key="lakers1" value="davis"/>
</map>
</property> <!--属性文件-->
<property name="properties">
<props>
<prop key="lakers">london</prop>
<prop key="lakers1">kuzma</prop>
</props>
</property>
</bean>
</beans>
  • 最后写一个测试的方法,对以上的注入进行测试
 package zh.test;

 import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import zh.test.deomo.UserService;
import zh.test.deomo.UserServiceImpl;
import zh.test.deomo1.DeptService;
import zh.test.deomo2.Car;
import zh.test.deomo4.Extension; public class testDemo {
@Test
public void test()
{
//原始写法:
UserServiceImpl userService = new UserServiceImpl();
userService.sayHi();
//使用Spring框架 } @Test
public void testSpring(){
//创建SpringIOC的工厂(容器) ,需要加载配置文件,就会将配置文件中的类通过反射的方式创建成对象
//将对象存入到容器中,以key : value 的方式,k="userService" v="UserServiceImpl对象(默认是单例的)"
//
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
//从容器中获取对象 面向接口的编程
UserService userService = (UserService) ac.getBean("userService");
//调用对象
userService.sayHi();
} // setter的方式注入
@Test
public void testSpringDI(){
//创建SpringIOC的工厂(容器) ,需要加载配置文件,就会将配置文件中的类通过反射的方式创建成对象
//将对象存入到容器中,以key : value 的方式,k="userService" v="UserServiceImpl对象(默认是单例的)"
//
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
//从容器中获取对象 面向接口的编程
DeptService deptService = (DeptService) ac.getBean("deptService");
//调用对象
deptService.save();
} // 属性的构造方法注入
@Test
public void testCar(){
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
//从容器中获取对象 面向接口的编程
Car car = (Car) ac.getBean("car");
//调用对象
System.out.println(car);
} //扩展方法 数组、集合、map、属性文件
@Test
public void testExtension(){
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
//从容器中获取对象 面向接口的编程
Extension extension = (Extension) ac.getBean("extension");
//调用对象
System.out.println(extension);
} }

分享之路,欢迎交流。。。

最新文章

  1. POJ 2251 Dungeon Master(3D迷宫 bfs)
  2. vim 快捷键
  3. 【转】MYSQL启用日志,和查看日志
  4. html --- canvas --- javascript --- 在线画板
  5. 微信浏览器如何禁止iPhone手机上下滑动网页
  6. jquery ui 改写cloes事件
  7. Colored Linux Man pages
  8. Map集合中value()方法与keySet()、entrySet()区别
  9. hdu1031
  10. 经常出现null错误之tostring
  11. hadoop记录-浅析Hadoop中的DistCp和FastCopy(转载)
  12. 关于oracle sql语句查询时表名和字段名要加双引号的问题详解
  13. MessageFormat.format 包含单引号引起的不可替换
  14. java中MD5加密
  15. .Net开源网络爬虫Abot介绍(转)
  16. python第三方库Requests的基本使用
  17. luogu P4161 [SCOI2009]游戏
  18. windows php7 安装 mongodb 扩展
  19. django 单元测试错误总结
  20. 20155233 2016-2017-2 《Java程序设计》第5周学习总结

热门文章

  1. 令人清爽的异步函数async、await
  2. 谁说编译器不SB
  3. The J2EE Architecture
  4. 论文学习——《Good View Hunting: Learning Photo Composition from Dense View Pairs》
  5. memset函数及其用法,C语言memset函数详解
  6. Redis探索之路(三):Redis的五种数据类型String和Hash
  7. TStringGrid 实现下拉框
  8. hive的复合数据类型
  9. java 直接调用micorosoft office (2003-2007和2010版本)中excel中计算函数
  10. NX二次开发-UFUN修剪体UF_MODL_trim_body