2.1.1 jar 包:java
此外,还加上commons-logging和log4j日志包spring
2.1.2用idea建立工程 数组
2.1.3勾选自动生成目录结构,不然要本身建立ide
2.1.4将jar包放入到项目中函数
2.1.5须要注意的是 若是是在test目录里面写测试代码,jar包必定也要添加到spring_test里面去,否者会一直报错建立不了bean的错误测试
a.在java下建立User类this
//1.建立对象 public class User { public void add(){ System.out.println("添加一个用户"); } }
b. 在resources下建立bean1.xml,添加以下idea
<!--ioc入门--> <!--2.配置对象--> <bean id="user" class="com.xiaobo.Bean.User" scope="prototype"></bean>
c. 在 test目录下建立TestIoc进行测试 ,点击运行spa
//3.测试 public class TestIoc { @Test public void test(){ //加载xml文件 ApplicationContext ac=new ClassPathXmlApplicationContext("bean1.xml"); User user = (User) ac.getBean("user"); user.add(); } }
a.建立类prototype
/** * 构造函数注入 */ public class PropertyDemo1 { public PropertyDemo1(String name) { this.name = name; } private String name; public void test(){ System.out.println("my name is :"+this.name); } }
b.配置文件
<!--构造函数完成属性注入--> <bean id="name" class="com.xiaobo.Property.PropertyDemo1"> <!--有参的构造函数,属性注入--> <constructor-arg value="jack" name="name" /> </bean>
c.测试
@Test public void test(){ //加载xml文件 ApplicationContext ac=new ClassPathXmlApplicationContext("bean1.xml"); PropertyDemo1 user = (PropertyDemo1) ac.getBean("name"); user.test(); }
a.建立类
public class Book { public void setName(String name) { this.name = name; } private String name; public void test(){ System.out.println("书名为: "+this.name); } }
b.配置文件
<!--set方法属性注入--> <bean id="book" class="com.xiaobo.Property.Book"> <!--用set方法完成属性注入,name为类中的成员变量--> <property name="name" value="易经经"></property> </bean>
c.测试
@Test public void test(){ //加载xml文件 ApplicationContext ac=new ClassPathXmlApplicationContext("bean1.xml"); PropertyDemo1 user = (PropertyDemo1) ac.getBean("name"); user.test(); }
public class BookService { public void setBookDao(BookDao bookDao) { this.bookDao = bookDao; } private BookDao bookDao; public void add(){ bookDao.add(); } }
public class BookDao { public void add(){ System.out.println("添加书籍 "); } }
b.配置文件
<!--对对象的注入--> <bean id="bookDao" class="com.xiaobo.Property.BookDao"> </bean> <bean id="bookService" class="com.xiaobo.Property.BookService"> <!--ref是上面定义的--> <property name="bookDao" ref="bookDao"></property> </bean>
c.测试
@Test public void test3(){ //加载xml文件 ApplicationContext ac=new ClassPathXmlApplicationContext("bean1.xml"); BookService user = (BookService) ac.getBean("bookService"); user.add(); }
a.建立类
public class Person { public void setArr(String[] arr) { this.arr = arr; } public void setMap(Map<String, String> map) { this.map = map; } public void setList(List<String> list) { this.list = list; } private String[] arr; private Map<String,String> map; private List<String> list; public void test(){ System.out.println("arr is "+arr); System.out.println("map is "+map); System.out.println("list is "+list); } }
b.配置文件
<bean id="arr" class="com.xiaobo.Property.Person"> <!--数组的注入--> <property name="arr"> <list> <value>123</value> <value>234</value> <value>34</value> <value>123</value> </list> </property> <property name="list"> <list> <value>asd</value> <value>zc</value> <value>vfs</value> <value>ghj</value> </list> </property> <property name="map"> <map> <entry key="abc" value="123"/> <entry key="456" value="再来一次"/> <entry key="10010" value="电信"/> </map> </property> </bean>
c.测试
@Test public void test4(){ //加载xml文件 ApplicationContext ac=new ClassPathXmlApplicationContext("bean1.xml"); Person user = (Person) ac.getBean("arr"); user.test(); }
b.1 建立新的xml文件 , xml配置,下面的代码会扫描具体包下的注解进行自动注入。
<!--开启注解扫描 在包里面属性,方法,类,是否有注解 --> <context:component-scan base-package="com.xiaobo"></context:component-scan>
b.2类中的注解
//业务层使用userService注解 @Service(value = "userService") public class UserService { //使用了注解注入后就不用使用set方法了,比xml注入便捷许多 //使用Autowired自动注入 // @Autowired,只要是有UserDao这个类,就会注入,跟@Repository(value = "userDao")无关 // private UserDao userDao; //指定@Repository(value = "userDao")的类 @Resource(name = "userDao") private UserDao userDao; public void add(){ userDao.add(); } }
//持久层的注解为Repository @Repository(value = "userDao") public class UserDao { public void add(){ System.out.println("UserDao add user"); } }
b.3测试
@org.junit.Test public void testService(){ ApplicationContext context=new ClassPathXmlApplicationContext("bean1.xml"); UserService user = (UserService) context.getBean("userService"); user.add(); }