摘要:本周开始学习spring框架
一、spring简介:
Spring框架是一个分层架构,由7个定义良好的模块组成。Spring 模块构建在核心容器之上,核心容器定义了创建、配置和管理 bean 的方式。
Spring 框架的 7 个模块
组成 Spring 框架的每个模块(或组件)都可以单独存在,或者与其他一个或多个模块联合实现。每个模块的功能如下:
BeanFactory
,它是工厂模式的实现。BeanFactory
使用控制反转 (IOC) 模式将应用程序的配置和依赖性规范与实际的应用程序代码分开。Spring 框架的功能可以用在任何 J2EE 服务器中,大多数功能也适用于不受管理的环境。Spring 的核心要点是:支持不绑定到特定 J2EE 服务的可重用业务和数据访问对象。毫无疑问,这样的对象可以在不同 J2EE 环境 (Web 或 EJB)、独立应用程序、测试环境之间重用。
二、spring配置bean
1、使用spring框架时,首先导入相关jar包
2、创建javabean,主函数
package com.spring; public class Product { private String name; private int num; private double price; public Product(String name, int num) { super(); this.name = name; this.num = num; } public Product(String name, double price) { super(); this.name = name; this.price = price; } @Override public String toString() { return "Product [name=" + name + ", num=" + num + ", price=" + price + "]"; } }
package com.spring; public class Person { private String name; private int age; private Product product; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Product getProduct() { return product; } public void setProduct(Product product) { this.product = product; } @Override public String toString() { return "Person [name=" + name + ", age=" + age + ", product=" + product + "]"; } public Person() { super(); // TODO 自动生成的构造函数存根 } public Person(String name, int age, Product product) { super(); this.name = name; this.age = age; this.product = product; } }
package com.spring; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args) { ApplicationContext c=new ClassPathXmlApplicationContext("app.xml"); Product p=(Product) c.getBean("product"); System.out.println(p); Product pp=(Product) c.getBean("product2"); System.out.println(pp); Person pe=(Person) c.getBean("person"); System.out.println(pe); Person pe2=(Person) c.getBean("person2"); System.out.println(pe2); } }
3、新建以下xml文件
4、编写.xml文件
<?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" 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 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd"> <bean id="product" class="com.spring.Product"> <constructor-arg value="zhang" index="0"></constructor-arg> <constructor-arg value="2" type="int"></constructor-arg> </bean> <bean id="product2" class="com.spring.Product"> <constructor-arg type="String"> <!--特殊字符 --> <value><![CDATA[<san?>]]></value> </constructor-arg> <constructor-arg type="double"> <value>400</value> </constructor-arg> </bean> <bean id="person" class="com.spring.Person"> <property name="name" value="tom"></property> <property name="age" value="10"></property> <!-- 引用其他bean --> <!-- <property name="product" ref="product2"></property> --> <!-- <ref bean="product2"/> --> <!-- 内部bean --> <property name="product"> <bean id="product" class="com.spring.Product"> <constructor-arg value="zhang" index="0"></constructor-arg> <constructor-arg value="2" type="int"></constructor-arg> </bean> </property> </bean> <bean id="person2" class="com.spring.Person"> <constructor-arg value="zhang"></constructor-arg> <constructor-arg value="1"></constructor-arg> <!-- 赋值null --> <constructor-arg> <null /> </constructor-arg> </bean> <!-- list集合属性 --> <bean id="person3" class="com.list.Person"> <property name="name" value="aim"></property> <property name="age" value="20"></property> <property name="products"> <list> <ref bean="product" /> <ref bean="product2" /> </list> </property> </bean> <!-- Map属性 --> <bean id="newperson" class="com.list.Person1"> <property name="name" value="jack"></property> <property name="age" value="18"></property> <property name="products"> <map> <entry key="A" value-ref="product"></entry> <entry key="B" value-ref="product2"></entry> </map> </property> </bean> <!--properties属性 --> <bean id="data" class="com.list.Data"> <property name="properties"> <props> <prop key="user">root</prop> <prop key="password">1234</prop> <prop key="driver">a</prop> </props> </property> </bean> <!--配置单列集合bean ,供多个bean使用--> <util:list id="products"> <ref bean="product" /> <ref bean="product2" /> </util:list> <bean id="person4" class="com.list.Person"> <property name="name" value="tomm"></property> <property name="age" value="11"></property> <property name="products" ref="products"></property> </bean> <!--通过p对bean的属性赋值--> <bean id="person5" class="com.list.Person" p:age="22" p:name="giao" p:products-ref="products"></bean> </beans>
为了区别list和map的使用,有重新写了Javabean和main
package com.list; import java.util.List; import com.spring.Product; public class Person { private String name; private int age; private List<Product> products; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public List<Product> getProducts() { return products; } public void setProducts(List<Product> products) { this.products = products; } @Override public String toString() { return "Person [name=" + name + ", age=" + age + ", products=" + products + "]"; } }
package com.list; import java.util.Map; import com.spring.Product; public class Person1 { private String name; private int age; private Map<String,Product> products; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Map<String, Product> getProducts() { return products; } public void setProducts(Map<String, Product> products) { this.products = products; } @Override public String toString() { return "Person1 [name=" + name + ", age=" + age + ", products=" + products + "]"; } }
package com.list; import java.util.Properties; public class Data { private Properties properties; public Properties getProperties() { return properties; } public void setProperties(Properties properties) { this.properties = properties; } @Override public String toString() { return "Data [properties=" + properties + "]"; } }
package com.list; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.list.Person; import com.list.Person1; public class Main { public static void main(String[] args) { ApplicationContext c=new ClassPathXmlApplicationContext("app.xml"); Person pe3=(Person) c.getBean("person3"); System.out.println(pe3); Person1 np=(Person1) c.getBean("newperson"); System.out.println(np); Data data=(Data) c.getBean("data"); System.out.println(data.getProperties()); Person pe4=(Person) c.getBean("person4"); System.out.println(pe4); Person pe5=(Person) c.getBean("person5"); System.out.println(pe5); } }
原文:https://www.cnblogs.com/MoooJL/p/12622055.html