第三次作业随想篇:Spring入门

1. 简单了解Spting的 AOP 和 IOC 概念
  AOP: Aspect Oriented Program, 面向(方面)切面的编程;Filter(过滤器)也是一种 AOP. AOP 是一种新的 方法论, 是对传统 OOP(Object-Oriented
    Programming, 面向对象编程) 的补充. AOP 的主要编程对象是切面(aspect),而切面模块化横切关注点.可以举例通过事务说明.

  IOC: Invert Of Control, 控制反转. 也成为 DI(依赖注入)其思想是反转资源获取的方向. 传统的资源查找方式要求组件向容器发起请求查找资源.作为
    回应, 容器适时的返回资源. 而应用了 IOC 之后, 则是容器主动地将资源推送给它所管理的组件,组件所要做的仅是选择一种合适的方式来接受资源. 这种行
    为也被称为查找的被动形式

2.在 Spring 中如何配置 Bean ?
  Bean 的配置方式: 通过全类名 (反射)、 通过工厂方法 (静态工厂方法 & 实例工厂方法)、FactoryBean
  
下面具一个简单地例子说明

首先基于配置的方式:

  xml文件:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xmlns:p="http://www.springframework.org/schema/p"
 5     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
 6 
 7    <bean id="address1" class="autowire.Address" 
 8    p:city="baoshan" p:street="lehong"></bean>
 9    
10    <bean id="address2" class="autowire.Address"
11    p:city="xiangtan" p:street="qiujier"></bean>
12    
13    <bean id="car1" class="autowire.Car"
14     p:brand="benchi" p:price="500000"></bean>
15    
16    <bean id="person1" class="autowire.Person" 
17    p:name="hepan1" p:address-ref="address2" p:car-ref="car1"></bean>
18 </beans>
View Code

相应的类文件:

    Address.java

 1 package autowire;
 2 
 3 public class Address {
 4     private String city;
 5     private String street;
 6     
 7     public String getCity() {
 8         return city;
 9     }
10     public void setCity(String city) {
11         this.city = city;
12     }
13     public String getStreet() {
14         return street;
15     }
16     public void setStreet(String street) {
17         this.street = street;
18     }
19     @Override
20     public String toString() {
21         return "Address [city=" + city + ", street=" + street + "]";
22     }
23     
24 }
View Code

  Car.java

 1 package autowire;
 2 
 3 public class Car {
 4    private String brand;
 5    private double price;
 6     public String getBrand() {
 7         return brand;
 8     }
 9     public void setBrand(String brand) {
10         this.brand = brand;
11     }
12     public double getPrice() {
13         return price;
14     }
15     public void setPrice(double price) {
16         this.price = price;
17     }
18     @Override
19     public String toString() {
20         return "Car [brand=" + brand + ", price=" + price + "]";
21     }
22     
23 }
View Code

      Person.java

 1 package autowire;
 2 
 3 public class Person {
 4    private String name;
 5    private Address  address;
 6    private Car car;
 7     public String getName() {
 8         return name;
 9     }
10     public void setName(String name) {
11         this.name = name;
12     }
13     public Address getAddress() {
14         return address;
15     }
16     public void setAddress(Address address) {
17         this.address = address;
18     }
19     public Car getCar() {
20         return car;
21     }
22     public void setCar(Car car) {
23         this.car = car;
24     }
25     @Override
26     public String toString() {
27         return "Person [name=" + name + ", address=" + address + ", car=" + car
28                 + "]";
29     }
30 
31        
32 }
View Code

      测试的main方法

 1 package autowire;
 2 
 3 import org.springframework.context.ApplicationContext;
 4 import org.springframework.context.support.ClassPathXmlApplicationContext;
 5 
 6 public class Main {
 7 
 8     public static void main(String[] args) {
 9         
10     ApplicationContext ctx=new ClassPathXmlApplicationContext("relation.xml");
11     Person person=(Person) ctx.getBean("person1");
12     System.out.println(person);
13     }
14 
15 }
View Code

     配置成功输出

 

 

 2.测试Bean的生命周期

  IOC 容器对 Bean 的生命周期:
    ①. 通过构造器或工厂方法创建 Bean 实例
    ②. 为 Bean 的属性设置值和对其他 Bean 的引用
    ③ . 将 Bean 实 例 传 递 给 Bean 后 置 处 理 器 的postProcessBeforeInitialization 方法
    ④. 调用 Bean 的初始化方法(init-method)
    ⑤ . 将 Bean 实 例 传 递 给 Bean 后 置 处 理 器 的postProcessAfterInitialization 方法
    ⑦. Bean 可以使用了
    ⑧. 当容器关闭时, 调用 Bean 的销毁方法(destroy-method)

首先建立beanlife.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
 5 
 6    <bean id="car" class="beanlife.Car" init-method="init" destroy-method="destory">
 7     <property name="brand" value="benchi"></property>
 8    </bean>
 9    
10    <bean class="beanlife.BeanpostProcessor">
11     
12    </bean>
13 </beans>
View Code

实现Bean的构造器

 1 package beanlife;
 2 
 3 import org.springframework.beans.BeansException;
 4 import org.springframework.beans.factory.config.BeanPostProcessor;
 5 
 6 public class BeanpostProcessor implements BeanPostProcessor {
 7 
 8     public Object postProcessAfterInitialization(Object arg0, String arg1)
 9             throws BeansException {
10         Car car=new Car();
11         car.setBrand("Foed");
12         System.out.println("postProcessAfterInitialization  "+arg1);
13         return car;
14     }
15 
16     public Object postProcessBeforeInitialization(Object arg0, String arg1)
17             throws BeansException {
18         System.out.println("postProcessBeforeInitialization  "+arg1);
19         return arg0;
20     }
21 
22 }
View Code

将要被实例化的类

 1 package beanlife;
 2 
 3 public class Car {
 4    @Override
 5     public String toString() {
 6         return "Car [brand=" + brand + "]";
 7     }
 8 
 9 Car()
10    {
11        System.out.println("bean被初始化-----------");
12    }
13    
14    public String getBrand() {
15     return brand;
16     }
17     public void setBrand(String brand) {
18          System.out.println("Car set");
19         this.brand = brand;
20     }
21 
22   private String brand;
23    
24    void init()
25    {
26        System.out.println("init");
27    }
28    void destory()
29    {
30        System.out.println("bean被销毁--------------");
31    }
32 }
View Code

主要测试Main方法

 1 package beanlife;
 2 
 3 import org.springframework.context.ApplicationContext;
 4 import org.springframework.context.support.AbstractApplicationContext;
 5 import org.springframework.context.support.ClassPathXmlApplicationContext;
 6 
 7 public class Main {
 8 
 9     
10     public static void main(String[] args) {
11         ApplicationContext ctx=new ClassPathXmlApplicationContext("beanlife.xml");
12         Car car=(Car) ctx.getBean("car");
13         System.out.println(car);
14         
15         ((AbstractApplicationContext) ctx).close();
16     }
17 
18 }
View Code

输出:

就可以看见bena被创建和销毁的过程了。

 

3.Spring与数据库的交互即配置相应的与数据库连接信息

  配置连接的信息

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
 5 
 6    <!-- bean的属性配置可以用来来连接数据库 ,数据连接池使用的是c3p0的数据源
 7        配置最基本的数据库连接有四个-->
 8    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
 9        <property name="user" value="scott"></property>
10        <property name="password" value="orcl"></property>
11        <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
12        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/itcasttax1"></property>
13        <property name="initialPoolSize" value="10"></property>
14         <!--连接池中保留的最小连接数。Default: 3 -->
15         <property name="minPoolSize" value="3"></property>
16         <!--连接池中保留的最大连接数。Default: 15 -->
17         <property name="maxPoolSize" value="10"></property>
18         <!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
19         <property name="acquireIncrement" value="3"></property>
20         <!--最大空闲时间,1800秒内未使用则连接被丢弃,若为0则永不丢弃。Default: 0 -->
21         <property name="maxIdleTime" value="1800"></property>
22    </bean>
23 </beans>
View Code

测试数据库是否连接成功的main方法

 1 package property;
 2 
 3 import java.sql.SQLException;
 4 
 5 import javax.sql.DataSource;
 6 
 7 import org.springframework.context.ApplicationContext;
 8 import org.springframework.context.support.ClassPathXmlApplicationContext;
 9 
10 public class Main {
11 
12     public static void main(String[] args) throws SQLException {
13     
14         ApplicationContext ctx=new ClassPathXmlApplicationContext("beans-property.xml");
15         DataSource dt=(DataSource) ctx.getBean("dataSource");
16         System.out.println(dt.getConnection());
17         System.out.println("数据库连接succeess。。。。。");
18     }
19 
20 }
View Code

连接成功则输出

表明数据库连接成功

 

posted @ 2016-03-20 14:54  2013551631何攀  阅读(204)  评论(0编辑  收藏  举报