java框架--Spring XML 配置基础(一)
1.代码区
// 测试import 导入entity.xml注入name值
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package cn.sxt.bean; public class Hello { public Hello() { System.out.println("hello 被创建"); } private String name; private boolean sex; public boolean isSex() { return sex; } public void setSex(boolean sex) { this.sex = sex; } public void setName(String name) { this.name = name; } public void show(){ System.out.println("hello,"+name); } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package cn.sxt.test; import org.springframework.beans.factory.BeanFactory; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import cn.sxt.bean.Hello; public class Test { public static void main(String[] args) { // 解析beans.xml文件 生成管理相应的bean对象 BeanFactory context = new ClassPathXmlApplicationContext("beans.xml"); Hello hello = (Hello) context.getBean("h1"); // Hello hello = (Hello)context.getBean("h3"); // Hello hello= context.getBean(Hello.class); hello.show(); } } /** * hello 被创建 * hello,张三 */
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- bean就是java对象 由spring来创建和管理 --> <!-- id是bean的标识符 要唯一 如果没有配置id,name默认标识符 如果配置id,又配置了name 那么name是别名 name可以设置多个别名 分隔符可以 是 空格 逗号 分号 class是bean的全限定名=包名+类名 如果不配置 id,和name 那么可以根据applicationContext.getBean(Class) 获取对象 --> <bean id="h1" name="hello h2,h3;h4" class="cn.sxt.bean.Hello"> <property name="name" value="张三"/> </bean> </beans>
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- bean就是java对象 由spring来创建和管理 --> <!-- id是bean的标识符 要唯一 如果没有配置id,name默认标识符 如果配置id,又配置了name 那么name是别名 name可以设置多个别名 分隔符可以 是 空格 逗号 分号 class是bean的全限定名=包名+类名 如果不配置 id,和name 那么可以根据applicationContext.getBean(Class) 获取对象 --> <import resource="config/spring/entity.xml"/> </beans>
// 测试 service层调用dao层的代码,采用bean注入的方式调用
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package cn.sxt.dao; public interface UserDao { public void getUser(); }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package cn.sxt.dao.impl; import cn.sxt.dao.UserDao; public class UserDaoMySqlImpl implements UserDao{ @Override public void getUser() { System.out.println("mysql获取用户数据"); } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package cn.sxt.dao.impl; import cn.sxt.dao.UserDao; public class UserDaoOracleImpl implements UserDao{ @Override public void getUser() { System.out.println("oracle获取用户数据"); } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package cn.sxt.service; public interface UserService { public void getUser(); }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package cn.sxt.service.impl; import cn.sxt.dao.UserDao; import cn.sxt.service.UserService; public class UserServiceImpl implements UserService{ private UserDao userDao=null; public void setUserDao(UserDao userDao) { this.userDao = userDao; } @Override public void getUser() { userDao.getUser(); } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package cn.sxt.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import cn.sxt.service.UserService; public class Test { public static void main(String[] args) { ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml"); UserService us = (UserService)ac.getBean("service"); us.getUser(); } } //oracle获取用户数据
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd" default-autowire="byName"> <bean id="mysqlDao" class="cn.sxt.dao.impl.UserDaoMySqlImpl"/> <!-- autowire自动装配 简化spring配置 no不使用自动装配 byName 根据名称(set方法名来的)去查找相应的bean,如果有则装配上 byType 根据类型进行自动装配 不用管bean的id.但是同一种类型的bean只能有一个。建议慎用 constructor 当通过构造器 注入 实例化bean时 适用byType的方式 装配构造方法 <bean id="service" class="cn.sxt.service.impl.UserServiceImpl" autowire="constructor"/> --> <bean id="mysqlDao" class="cn.sxt.dao.impl.UserDaoMySqlImpl"/> <bean id="oralceDao" class="cn.sxt.dao.impl.UserDaoOracleImpl"/> <bean id="service" class="cn.sxt.service.impl.UserServiceImpl"> <!-- ref引用对象(对象是由spring来创建的) --> <property name="userDao" ref="oralceDao"></property> </bean> </beans>
\
//测试通过构造注入属性和 userdynamicfactory依赖user 对象, 工厂设计模式
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package cn.sxt.factory; import java.util.List; import java.util.Map; import java.util.Set; import cn.sxt.vo.User; public class UserDynamicFactory { public User newInstance(String name){ return new User(name); } private int age; private List hobby; private Map friends; private Set set; private boolean ifMarried; public UserDynamicFactory(int age, List hobby, Map friends, Set set, boolean ifMarried) { super(); this.age = age; this.hobby = hobby; this.friends = friends; this.set = set; this.ifMarried = ifMarried; } @Override public String toString() { return "UserDynamicFactory [age=" + age + ", hobby=" + hobby + ", friends=" + friends + ", set=" + set + ", ifMarried=" + ifMarried + "]"; } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package cn.sxt.vo; public class User { public User(String name) { super(); this.name = name; } private String name; public void show(){ System.out.println("name="+name); } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package cn.sxt.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import cn.sxt.factory.UserDynamicFactory; import cn.sxt.vo.User; public class Test { public static void main(String[] args) { ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml"); //通过构造可以注入 UserDynamicFactory dynamicFactory = (UserDynamicFactory)ac.getBean("userFacotry"); System.out.println(dynamicFactory); // User user = (User)ac.getBean("user1"); user.show(); } } /** * UserDynamicFactory [age=25, hobby=[a, b, c], friends={liuhua=man, xujinglei=female}, set=[d, e, f], ifMarried=true] *name=王五 * */
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> //依赖的关系是 userdynamicfactory 依赖user 对象 <bean id="users" factory-bean="userFacotry" factory-method="newInstance"> // 代表构造函数的顺序是0开始 <constructor-arg index="0" value="王五"/> </bean> <!-- 设置别名 --> <alias name="users" alias="user1"/> <bean id="userFacotry" class="cn.sxt.factory.UserDynamicFactory"> <constructor-arg index="0" value="25"></constructor-arg> <constructor-arg> <list> <value>a</value> <value>b</value> <value>c</value> </list> </constructor-arg> <constructor-arg> <set> <value>d</value> <value>e</value> <value>f</value> </set> </constructor-arg> <constructor-arg> <map> <entry key="liuhua" value="man"></entry> <entry key="xujinglei" value="female"></entry> </map> </constructor-arg> <constructor-arg index="4" value="true"> </constructor-arg> </bean> </beans>
// 通过property 方式注入值
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package cn.sxt.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import cn.sxt.vo.Student; import cn.sxt.vo.User; public class Test { public static void main(String[] args) { ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml"); //得到student对象值 Student student=(Student) ac.getBean("student"); student.show(); System.out.println("+++++++++++++++++++++++++++++++++++++"); User user1 = (User)ac.getBean("user"); System.out.println(user1); System.out.println("+++++++++++++++++++++++++++++++++++++"); User user = (User)ac.getBean("u1"); System.out.println(user); } } /* name=张三丰 addr=北京西三旗 books= 傲慢与偏见 仲夏夜之梦 雾都孤儿 hobbies=[羽毛球, 乒乓球, 玻璃球, 台球球] cards={中国银行=149127348932174, 建设银行=622710023478234234} games=[lol, dota, cs1.6, dnf] wife=null info={学号=2015052601, name=小明, sex=男} +++++++++++++++++++++++++++++++++++++ User [name=风清扬, age=230] +++++++++++++++++++++++++++++++++++++ User [name=nico, age=16] */
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package cn.sxt.vo; public class Address { private String address; public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package cn.sxt.vo; import java.util.List; import java.util.Map; import java.util.Properties; import java.util.Set; public class Student { private String name; private Address addr; private String[] books; private List<String> hobbies; private Map<String,String> cards; private Set<String> games; private String wife; private Properties info; public void setName(String name) { this.name = name; } public void setAddr(Address addr) { this.addr = addr; } public void setBooks(String[] books) { this.books = books; } public void setHobbies(List<String> hobbies) { this.hobbies = hobbies; } public void setCards(Map<String, String> card) { this.cards = card; } public void setGames(Set<String> games) { this.games = games; } public void setWife(String wife) { this.wife = wife; } public void setInfo(Properties info) { this.info = info; } public void show(){ System.out.println("name="+name+" addr="+addr.getAddress()); System.out.println("books="); for(int i=0;i<books.length;i++){ System.out.print(books[i]+" "); } System.out.println(); System.out.println("hobbies="+hobbies); System.out.println("cards="+cards); System.out.println("games="+games); System.out.println("wife="+wife); System.out.println("info="+info); } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package cn.sxt.vo; public class User { private String name; private int age; public User() { // TODO Auto-generated constructor stub } public User(String name, int age) { super(); this.name = name; this.age = age; } 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; } @Override public String toString() { return "User [name=" + name + ", age=" + age + "]"; } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<?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" xmlns:c="http://www.springframework.org/schema/c" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- bean的作用域 singleton单列 整个容器中只有一个对象实例 默认是单列 prototype原型 每次获取bean都产生一个新的对象 request 每次请求时创建一个新的对象 session 在会话的范围内时一个对象 global session 只在portlet下有用,表示是application application 在应用范围中一个对象 --> <bean id="addr" class="cn.sxt.vo.Address" scope="singleton"> <property name="address" value="北京西三旗"/> </bean> <bean id="student" class="cn.sxt.vo.Student"> <property name="name" value="张三丰"/> <property name="addr" ref="addr"/> <property name="books"> <array> <value>傲慢与偏见</value> <value>仲夏夜之梦</value> <value>雾都孤儿</value> </array> </property> <property name="hobbies"> <list> <value>羽毛球</value> <value>乒乓球</value> <value>玻璃球</value> <value>台球球</value> </list> </property> <property name="cards"> <map> <entry key="中国银行" value="149127348932174"/> <entry> <key><value>建设银行</value></key> <value>622710023478234234</value> </entry> </map> </property> <property name="games"> <set> <value>lol</value> <value>dota</value> <value>cs1.6</value> <value>dnf</value> </set> </property> <property name="wife"><null/></property> <property name="info"> <props> <prop key="学号">2015052601</prop> <prop key="sex">男</prop> <prop key="name">小明</prop> </props> </property> </bean> <!-- p命名空间注入 属性依然要设置set方法 --> <bean id="user" class="cn.sxt.vo.User" p:name="风清扬" p:age="230"/> <!-- c命名空间注入要求有对应参数的构造方法 --> <bean id="u1" class="cn.sxt.vo.User" c:name="nico" c:age="16"/> </beans>