标签:frame tool 名称空间 处理 context let 重用 类对象 command
| 
 1 
2 
3 
4 
5 
6 
 | 
<!-- class指定类对象的全类名,交给服务器创建一个对象   id是一个唯一标识,在IOC只能出现一个id为book的bean对象 --><bean id="book" class="com.neuedu.spring.bean.Book">      <property name="bookName" value="JAVA"></property>      <property name="author" value="you"></property>      <property name="price" value="123.123"></property></bean> | 
| 
 1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
 | 
public class TestIOC {      @Test      public void test() {            //1.获取IOC容器本身这个对象            ApplicationContext ioc = new ClassPathXmlApplicationContext("applicationContext.xml");            //2.从IOC容器中获取bean对象            Object bean = ioc.getBean("book");            System.out.println(bean);      }} | 
| 
 1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
 | 
public class TestIOC {      @Test      public void test() {            //1.获取IOC容器本身这个对象            ApplicationContext ioc = new ClassPathXmlApplicationContext("applicationContext.xml");            //2.从IOC容器中获取bean对象            Book bean = ioc.getBean(Book.class);            System.out.println(bean.getBookName()+"---"+bean.getAuthor()+"---"+bean.getPrice());      }} | 
要注意,创建实体类的时候要有无参构造器,没有无参的话会默认有一个无参构造器,但是这时要是有一个有参构造器,就构建不成对象,获取不到bean实例。
| 
 1 
2 
3 
4 
5 
 | 
<bean id="book01" class="com.neuedu.spring.bean.Book">     <constructor-arg value="you" index="1"></constructor-arg>     <constructor-arg value="C++" index="0"></constructor-arg>     <constructor-arg value="22.22" index="2" type="double"></constructor-arg></bean> | 

| 
 1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
 | 
public class StudentController {      private StudentService studentService;      public StudentService getStudentService() {            return studentService;      }      public void setStudentService(StudentService studentService) {            this.studentService = studentService;      }      @Override      public String toString() {            return "StudentController [studentService=" + studentService + "]";      }} | 
| 
 1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
 | 
public class StudentService {      private StudentDao studentDao;      public StudentDao getStudentDao() {            return studentDao;      }      public void setStudentDao(StudentDao studentDao) {            this.studentDao = studentDao;      }      @Override      public String toString() {            return "StudentService [studentDao=" + studentDao + "]";      }} | 
| 
 1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
 | 
public class StudentDao {      private String username;      public String getUsername() {            return username;      }      public void setUsername(String username) {            this.username = username;      }      @Override      public String toString() {            return "StudentDao [username=" + username + "]";      }} | 
| 
 1 
2 
3 
4 
5 
6 
7 
8 
 | 
<!-- 给bean的级联属性赋值 --><bean id="studentDao" class="com.neuedu.spring.bean.StudentDao"></bean><bean id="studentService" class="com.neuedu.spring.bean.StudentService"></bean><bean id="studentController" class="com.neuedu.spring.bean.StudentController">      <property name="studentService" ref="studentService"></property>      <property name="studentService.studentDao" ref="studentDao"></property>      <property name="studentService.studentDao.username" value="zhangsan"></property></bean> | 
| 
 1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
 | 
public class TestIOC {      @Test      public void test() {            //1.获取IOC容器本身这个对象            ApplicationContext ioc = new ClassPathXmlApplicationContext("applicationContext.xml");            //2.从IOC容器中获取bean对象            Object bean = ioc.getBean("studentController");            System.out.println(bean);      }} | 
| 
 1 
2 
3 
4 
5 
6 
7 
 | 
<!-- 通过p名称空间为bean赋值 --><bean id="student" class="com.neuedu.spring.bean.Student"     p:name="zhangsan"     p:gender="1"     p:address="China"></bean> | 
| 
 1 
2 
3 
 | 
<!-- 测试bean的作用域,分别创建单实例和多实例的bean --><bean id="book2" scope="singleton" class="com.neuedu.spring.bean.Book"></bean><bean id="student2" scope="prototype" class="com.neuedu.spring.bean.Student"></bean> | 
显示 一个book ,三个 student
| 
 1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
 | 
public class TestIOC {      //1.获取IOC容器本身这个对象      private ApplicationContext ioc = new ClassPathXmlApplicationContext("applicationContext.xml");      @Test      public void test() {            ioc.getBean("book");            ioc.getBean("book");            ioc.getBean("book");            ioc.getBean("student");            ioc.getBean("student");            ioc.getBean("student");      }} | 
| 
 1 
2 
 | 
<bean id="book" class="com.neuedu.spring.bean.Book"></bean><bean id="student" class="com.neuedu.spring.bean.Student"></bean> | 
| 
 1 
2 
3 
4 
5 
6 
7 
8 
9 
 | 
public class TestIOC {      private ApplicationContext ioc = new ClassPathXmlApplicationContext("applicationContext.xml");      @Test      public void test() {      }} | 
| 
 1 
 | 
<bean id="teacher" class="com.neuedu.spring.bean.Teacher" init-method="teacherinit" destroy-method="destroy"></bean> | 
但是调用不了 destroy 方法,通过 ApplicationContext 的子类的 close 方法调用 destroy 方法
| 
 1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
 | 
public class TestIOC {      private ApplicationContext ioc = new ClassPathXmlApplicationContext("applicationContext.xml");      @Test      public void test() {            ConfigurableApplicationContext cac = (ConfigurableApplicationContext) ioc;            ioc.getBean("teacher");            cac.close();      }} | 
| 
 1 
2 
3 
 | 
<bean id="book" class="com.neuedu.spring.bean.Book" depends-on="student"></bean><bean id="student" class="com.neuedu.spring.bean.Student" ></bean><bean id="teacher" class="com.neuedu.spring.bean.Teacher" init-method="teacherinit" destroy-method="destroy"></bean> | 
| 
 1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
 | 
<bean id="book" class="com.neuedu.spring.bean.Book">     <property name="bookName" value="JAVA"></property>     <property name="author" value="you"></property>     <property name="price" value="43.43"></property>     <property name="isbn" value="home"></property></bean><bean id="book1" class="com.neuedu.spring.bean.Book" parent="book">     <property name="price" value="34.23"></property>     <property name="isbn" value="house"></property></bean> | 
| 
 1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
 | 
<bean id="book" class="com.neuedu.spring.bean.Book" abstract="true">     <property name="bookName" value="JAVA"></property>     <property name="author" value="you"></property>     <property name="price" value="43.43"></property>     <property name="isbn" value="home"></property></bean><bean id="book1" parent="book">      <property name="price" value="43.43"></property>      <property name="isbn" value="home"></property></bean> | 
| 
 1 
2 
3 
4 
5 
 | 
<bean id="book" class="com.neuedu.spring.bean.Book">     <property name="bookName" value="java"></property>     <property name="price" value="33.5"></property>     <property name="isbn" value="house"></property></bean> | 
也可以这么写,将<null/> 写进<property> 中
| 
 1 
2 
3 
4 
5 
6 
7 
8 
 | 
<bean id="book" class="com.neuedu.spring.bean.Book">     <property name="bookName" value="java"></property>     <property name="author">            <null/>     </property>     <property name="price" value="33.5"></property>     <property name="isbn" value="house"></property></bean> | 
| 
 1 
2 
3 
4 
5 
6 
7 
8 
 | 
<bean id="book" class="com.neuedu.spring.bean.Book">     <property name="bookName" value="java"></property>     <property name="isbn" value="house"></property></bean><bean id="bookShop" class="com.neuedu.spring.bean.BookShop">     <property name="address" value="河北"></property>     <property name="book" ref="book"></property></bean> | 
| 
 1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
 | 
<bean id="bookShop" class="com.neuedu.spring.bean.BookShop">    <property name="address" value="河北"></property>    <property name="book">           <bean class="com.neuedu.spring.bean.Book">                 <property name="bookName" value="java"></property>                 <property name="author" value="you"></property>                 <property name="price" value="43.32"></property>                 <property name="isbn" value="house"></property>           </bean>     </property></bean> | 
| 
 1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
 | 
<bean id="book1" class="com.neuedu.spring.bean.Book">     <property name="bookName" value="java"></property></bean><bean id="book2" class="com.neuedu.spring.bean.Book">      <property name="author" value="you"></property></bean><bean id="book3" class="com.neuedu.spring.bean.Book">      <property name="price" value="43.43"></property></bean><bean id="book4" class="com.neuedu.spring.bean.Book">      <property name="isbn" value="home"></property></bean><bean id="bookShop" class="com.neuedu.spring.bean.BookShop">      <property name="bookList">           <list>                 <ref bean="book1"/>                 <ref bean="book2"/>                 <ref bean="book3"/>                 <ref bean="book4"/>            </list>      </property></bean> | 
| 
 1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
 | 
public class TestIOC {      private ApplicationContext ioc = new ClassPathXmlApplicationContext("applicationContext.xml");      @Test      public void test() {            BookShop bean = ioc.getBean(BookShop.class);            List<Book> bookList = bean.getBookList();            for(Book book : bookList){                  System.out.println(book);            }      }} | 
 
| 
 1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 
25 
26 
27 
28 
29 
30 
 | 
<bean id="bookShop" class="com.neuedu.spring.bean.BookShop">     <property name="bookMap">           <map>               <entry>                     <key>                          <value>book1</value>                     </key>                     <ref bean="book1"/>                </entry>                <entry>                      <key>                           <value>book2</value>                       </key>                       <ref bean="book2"/>                 </entry>                 <entry>                        <key>                            <value>book3</value>                        </key>                        <ref bean="book3"/>                  </entry>                  <entry>                         <key>                             <value>book4</value>                         </key>                         <ref bean="book4"/>                  </entry>            </map>      </property></bean> | 
用 .entry 遍历 map 集合
| 
 1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
 | 
public class TestIOC {      private ApplicationContext ioc = new ClassPathXmlApplicationContext("applicationContext.xml");      @Test      public void test() {            BookShop bean = ioc.getBean(BookShop.class);            Map<String, Book> bookMap = bean.getBookMap();            Set<Entry<String,Book>> entrySet = bookMap.entrySet();            for (Entry<String, Book> entry : entrySet) {                  System.out.println(entry.getKey()+"==="+entry.getValue());            }      }} | 
| 
 1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
 | 
<bean id="bookShop" class="com.neuedu.spring.bean.BookShop">      <property name="p">            <props>                 <prop key="book1">value1</prop>                 <prop key="book2">value2</prop>                 <prop key="book3">value3</prop>                 <prop key="book4">value4</prop>            </props>      </property></bean> | 
| 
 1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
 | 
public class TestIOC {      private ApplicationContext ioc = new ClassPathXmlApplicationContext("applicationContext.xml");      @Test      public void test() {            BookShop bean = ioc.getBean(BookShop.class);            java.util.Properties p = bean.getP();            System.out.println(p.get("book1"));            System.out.println(p.get("book2"));            System.out.println(p.get("book3"));            System.out.println(p.get("book4"));      }} | 
Spring(一)--作用、IOC容器细节、搭配环境、Spring实验 (转)
标签:frame tool 名称空间 处理 context let 重用 类对象 command
原文地址:http://www.cnblogs.com/zuiai/p/7442122.html