标签:选中 创建 code index work 框架 test display void
目录结构:
Spring是一个容器框架,用于配置bean并且维护bean之间关系的框架。
Spring的结构图:
我的结构目录:
package com.server; public class User { private String name; public void getName() { System.out.println("Hello "+name); } public void setName(String name) { this.name = name; } }
package com.test; /* *引入ApplicationContext和ClassPathXmlApplication */ import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.server.User; public class Test { public static void main(String[] args) { ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml"); User user = (User)ac.getBean("user");//根据Bean的id来识别 user.getName(); } }
ApplicationContext是一个接口,由ClassPathXmlApplicationContext寻找applicationContext这个文件,在开发包里一个index.html的网页文件,读者可以在里面查找相应Spring版本的API文档。
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:cache="http://www.springframework.org/schema/cache" xsi:schemaLocation=" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> <!-- 在容器文件中配置bean(service/dao.domain/action/数据原); --> <!-- bean的作用是,当我们的Spring框架加载的时候,spring就会自动创建一个bean对象,并且放入内存。id是标识符,class指定路径 本例相当于: User user=new User(); user.setName(""chauncy); --> <bean id="user" class="com.server.User"> <property name="name"> <value>chauncy</value> </property> </bean>
</beans>
http://blog.csdn.net/zoutongyuan/article/details/27073683
本文为博主原创文章,如需转载请注明出处。
标签:选中 创建 code index work 框架 test display void
原文地址:http://www.cnblogs.com/HDK2016/p/6217274.html