|
第七周 |
所花时间 |
25h左右 |
代码量 |
1500行左右 |
博客量 |
3篇 |
学到的知识点 |
spring中的自动装配,bean之间的关系,bean的作用域,引用外部属性文件
|
一、自动装配
package zidongpeizhi; public class Address { private String city; private String home; public String getCity() { return city; } public void setCity(String city) { this.city = city; } public String getHome() { return home; } public void setHome(String home) { this.home = home; } @Override public String toString() { return "Address [city=" + city + ", home=" + home + "]"; } }
package zidongpeizhi; public class Car { private String name; private double price; public String getName() { return name; } public void setName(String name) { this.name = name; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } @Override public String toString() { return "Car [name=" + name + ", price=" + price + "]"; } }
package zidongpeizhi; public class Person { private String name; private Car cars; private Address address; public String getName() { return name; } public void setName(String name) { this.name = name; } public Car getCars() { return cars; } public void setCars(Car cars) { this.cars = cars; } public Address getAddress() { return address; } public void setAddress(Address address) { this.address = address; } @Override public String toString() { return "Person [name=" + name + ", cars=" + cars + ", address=" + address + "]"; } }
package zidongpeizhi; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import zidongpeizhi.Person; public class Main { public static void main(String[] args) { ApplicationContext c=new ClassPathXmlApplicationContext("zidongpeizhi.xml"); Person pe=(Person) c.getBean("person"); System.out.println(pe); } }
<?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:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="address" class="zidongpeizhi.Address" p:city="ningxia" p:home="guang"></bean> <bean id="car" class="zidongpeizhi.Car" p:name="dazhong" p:price="60"></bean> <!-- <bean id="person" class="zidongpeizhi.Person" p:name="tom" p:address-ref="address" p:cars-ref="car"></bean> --> <!--byname 匹配javabean里的setter <bean id="person" class="zidongpeizhi.Person" p:name="tom" autowire="byName"></bean> --> <!-- bytype ioc容器只能有一个匹配 --> <bean id="person" class="zidongpeizhi.Person" p:name="mmom" autowire="byType"></bean> </beans>
二、bean之间的关系
<?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:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- 设置abstract="true"属性,只能被继承 --> <bean id="address" p:city="beijing" p:home="z" abstract="true"> </bean> <!-- 继承address --> <bean id="address1" class="beanguanxi.Address" p:city="beijing" parent="address"> </bean> <!-- 依赖 --> <bean id="car" class="beanguanxi.Car" p:name="baoma" p:price="100"></bean> <bean id="person" class="beanguanxi.Person" p:name="tom" p:address-ref="address1" depends-on="car"> </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" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd"> <!-- 导入外部文件 --> <context:property-placeholder location="classpath:mysql"/> <bean id="datasource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="user" value="${user}"></property> <property name="password" value="${password}"></property> <property name="driverClass" value="${driverclass}"></property> <property name="jdbcUrl" value="${jdbcurl}"></property> </bean> <!-- <bean id="datasource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="user" value="root"></property> <property name="password" value="123456"></property> <property name="driverClass" value="com.mysql.jdbc.Driver"></property> <property name="jdbcUrl" value="jdbc:mysql:///mmm"></property> </bean> --> </beans>
user=root
password=123456
driverclass=com.mysql.jdbc.Driver
jdbcurl=jdbc:mysql:///mmm
package shujuyuan; import java.sql.SQLException; import javax.sql.DataSource; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args) { ApplicationContext c=new ClassPathXmlApplicationContext("shujuyuan.xml"); DataSource datasource =(DataSource) c.getBean("datasource"); try { System.out.println(datasource.getConnection()); } catch (SQLException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } } }