`
2277259257
  • 浏览: 515150 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

Spring----归纳--亲测

 
阅读更多

 

 

public class User {

	private String name;
	private int 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;
	}
	
}

 

 

public interface UserService {

	public User getServiceUser();
}

 

public class UserServiceImpl implements UserService {

	private User user;
	int age;
	String name;
	private List<User> listUser;
	private Map<Object,Object> mapUser;
	private Set<?> setUser;
	private Properties properties;
	/**
	 * 默认构造器<setter注入>
	 */
	public UserServiceImpl(){
		System.out.println("默认构造器。。。");
	}
	/**
	 * 自定义构造器<构造器注入>
	 */
	public UserServiceImpl(String name,User user,int age) {
		this.user=user;
		this.age=age;
		this.name=name;
		
		System.out.println("自定义构造器。。。。");
	}
	
	/**
	 * 自定义实例化方法<static工厂方法返回对象实例>
	 */
	public static UserServiceImpl createInstance (String name,User user,int age) {

		UserServiceImpl eb = new UserServiceImpl (name,user,age);
		
		System.out.println("自定义实例化方法。。。");
                return eb;
        }

	@Override
	public User getServiceUser() {
		// TODO Auto-generated method stub
		User user =new User();
		user.setAge(30);
		user.setName("接口获取");
		return user;
	}

	public void init(){
		System.out.println("初始化。。。。");
	}


	public User getUser() {
		return user;
	}


	public void setUser(User user) {
		this.user = user;
		System.out.println("setUser。。。。");
	}


	public void setAge(int age) {
		this.age = age;
	}


	public void setName(String name) {
		this.name = name;
	}
	public List<User> getListUser() {
		return listUser;
	}
	public void setListUser(List<User> listUser) {
		this.listUser = listUser;
		System.out.println("setListUser。。。。");
	}
	public Map<Object, Object> getMapUser() {
		return mapUser;
	}
	public void setMapUser(Map<Object, Object> mapUser) {
		this.mapUser = mapUser;
	}
	public Set<?> getSetUser() {
		return setUser;
	}
	public void setSetUser(Set<?> setUser) {
		this.setUser = setUser;
	}
	public Properties getProperties() {
		return properties;
	}
	public void setProperties(Properties properties) {
		this.properties = properties;
	}
	
}

 

<?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-2.5.xsd">


  <bean id="user" class="javaP.User">
  	<property name="age" value="100"></property>
  	<property name="name" value="king"></property>
        <property name="name"><value/></property><!--空字符串-->
	<property name="name"><null/></property><!--null-->
  </bean> 

<!-- 构造器注入 -->
<bean id="userService" class="javaP.UserServiceImpl" init-method="init">
	<constructor-arg type="java.lang.String" value="kkkkk" index="0"></constructor-arg>
	
	<!-- 两种等价,后者覆盖前者 -->
	<constructor-arg index="1">
		<bean class="javaP.User"></bean>
	</constructor-arg>
	<constructor-arg index="1" ref="user"></constructor-arg>
	
	<constructor-arg type="int" value="78000" index="2"></constructor-arg>
</bean>

<!-- static工厂方法返回对象实例 -->
<bean id="userService2" class="javaP.UserServiceImpl" factory-method="createInstance">
	<constructor-arg type="java.lang.String" value="工厂方法返回实例" index="0"></constructor-arg>
	
	<!-- 三种等价,后者覆盖前者 -->
	<constructor-arg index="1" ref="user"></constructor-arg>
	<constructor-arg index="1">
<!-- 		<ref bean="user"/> -->
		<ref local="user"/>
	</constructor-arg>
	<constructor-arg index="1">
		<bean class="javaP.User">
			<property name="name" value="覆盖"></property>
		</bean>
	</constructor-arg>
	
	<constructor-arg type="int" value="78000" index="2"></constructor-arg>
</bean>

<!-- setter注入 -->
<bean id="userService3" class="javaP.UserServiceImpl">
	<!-- map的key或value值,或set的value值还可以是以下元素:bean | ref | idref | list | set | map | props | value | null -->
	
	<!-- 注入对象 -->
	<!-- <property name="user" ref="user"></property> -->
	<property name="user">
		<ref local="user"/>
	</property>
	<!-- 注入变量 -->
	<property name="name" value="xxxx"></property>
	<property name="age" value="18"></property>
	
	<!-- 注入list集合 -->
	<property name="listUser">
		<list>
			<ref bean="user"/>
			<ref bean="user"/>
		</list>
	</property>
	
	<!-- 注入Map集合 -->
	<property name="mapUser">
		<map>
			<entry>
				<key>
					<value>实体1key</value>
				</key>
				<value>实体1value</value>
			</entry>
			<entry>
				<key>
					<value>实体2key</value>
				</key>
				<ref local="user"/>
			</entry>
			<entry>
				<key><null></null></key>
				<value></value>
			</entry>
                        <entry key="two" value="50"/>
		</map>
	</property>
	
	<!-- 注入Set集合 -->
	<property name="setUser">
	    <set>
	        <ref bean="user" />
	        <value>第二个对象</value>
	        <map>
	        	<entry>
	        		<key><value>map對象key</value></key>
	        		<value>map對象value</value>
	        	</entry>
	        </map>
	        <list>
	        	<ref bean="user"/>
			<ref bean="user"/>
	        </list>
	    </set>
	</property>
	
	<!-- 注入Properties值 -->
	<property name="properties">
	    <props>
	        <prop key="key1">key1@example.org</prop>
	        <prop key="key2">key2@example.org</prop>
	        <prop key="key3">key3@example.org</prop>
	    </props>
	</property>
	
	
</bean>

</beans>

 

public class UserControll {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"configer.xml"});
//		BeanFactory context =new XmlBeanFactory(new FileSystemResource("D:/JAVA/Workspace2/javaP/src/configer.xml"));
		
		System.out.println("控制反转实例化bean==============");
		UserService userService=(UserService)context.getBean("userService");
		User user=(User)userService.getServiceUser();
		System.out.println(user.getAge());
		System.out.println(user.getName());
		
		System.out.println("构造器注入==============");
		UserServiceImpl userServiceImpl=(UserServiceImpl)context.getBean("userService");
		user=(User)userServiceImpl.getUser();
		System.out.println(user.getAge());
		System.out.println(user.getName());
		System.out.println(userServiceImpl.age);
		System.out.println(userServiceImpl.name);
		
		System.out.println("static工厂方法返回对象实例==============");
		UserServiceImpl userServiceImpl2=(UserServiceImpl)context.getBean("userService2");
		user=(User)userServiceImpl2.getUser();
		System.out.println(user.getAge());
		System.out.println(user.getName());
		System.out.println(userServiceImpl2.age);
		System.out.println(userServiceImpl2.name);
		
		System.out.println("setter注入==============");
		UserServiceImpl userServiceImpl3=(UserServiceImpl)context.getBean("userService3");
		user=(User)userServiceImpl3.getUser();
		System.out.println(user.getAge());
		System.out.println(user.getName());
		System.out.println(userServiceImpl3.age);
		System.out.println(userServiceImpl3.name);
		
		System.out.println("setter注入List集合==============");
		System.out.println(userServiceImpl3.getListUser());
		
		System.out.println("setter注入Map集合==============");
		System.out.println(userServiceImpl3.getMapUser());
		
		System.out.println("setter注入Set集合==============");
		System.out.println(userServiceImpl3.getSetUser());
		
		System.out.println("setter注入Properties值==============");
		System.out.println(userServiceImpl3.getProperties());
		
                System.out.println("关闭IOC容器。。。。。。。。。。。。。。。");
		AbstractApplicationContext abcontext=(AbstractApplicationContext)context;
		abcontext.registerShutdownHook();
	}

}

 

 

 

 

 

 

 

 

 

 

 

分享到:
评论

相关推荐

    spring-context.jar

    spring-context-1.2.8.jar, spring-context-1.2.9.jar, spring-context-2.0-m2.jar, spring-context-2.0.1.jar, spring-context-2.0.2.jar, spring-context-2.0.4.jar, spring-context-2.0.6.jar, spring-context-...

    开发工具 spring-web-4.3.6.RELEASE

    开发工具 spring-web-4.3.6.RELEASE开发工具 spring-web-4.3.6.RELEASE开发工具 spring-web-4.3.6.RELEASE开发工具 spring-web-4.3.6.RELEASE开发工具 spring-web-4.3.6.RELEASE开发工具 spring-web-4.3.6.RELEASE...

    spring-aop.jar各个版本

    spring-aop-1.1.1.jar spring-aop-1.2.6.jar spring-aop-1.2.9.jar spring-aop-2.0.2.jar spring-aop-2.0.6.jar spring-aop-2.0.7.jar spring-aop-2.0.8.jar spring-aop-2.0.jar spring-aop-2.5.1.jar spring-aop-...

    开发工具 框架JAR spring-framework-4.3.6.RELEASE-dist

    开发工具 框架JAR spring-framework-4.3.6.RELEASE-dist开发工具 框架JAR spring-framework-4.3.6.RELEASE-dist开发工具 框架JAR spring-framework-4.3.6.RELEASE-dist开发工具 框架JAR spring-framework-4.3.6....

    spring-core-x.x.x.jar 包下载

    spring-core-4.1.6.RELEASE.jar spring-core-4.3.10.RELEASE.jar spring-core-4.3.12.RELEASE.jar spring-core-4.3.13.RELEASE.jar spring-core-4.3.14.RELEASE.jar spring-core-4.3.16.RELEASE.jar spring-core-4.3...

    spring-ai-core-0.8.1

    spring-ai-core 0.8.1,解决大家使用2023.0.1.0 版本 Spring Cloud Alibaba 依赖,代码依赖下载报错问题, &lt;groupId&gt;com.alibaba.cloud&lt;/groupId&gt; &lt;artifactId&gt;spring-cloud-alibaba-dependencies &lt;version&gt;...

    springfox-spring-web-2.9.2-API文档-中文版.zip

    赠送jar包:springfox-spring-web-2.9.2.jar; 赠送原API文档:springfox-spring-web-2.9.2-javadoc.jar; 赠送源代码:springfox-spring-web-2.9.2-sources.jar; 赠送Maven依赖信息文件:springfox-spring-web-...

    spring-cglib-repack-3.2.0.jar和spring-objenesis-repack-2.1.jar

    在这个问题中,我们遇到了两个关键的jar包:`spring-cglib-repack-3.2.0.jar`和`spring-objenesis-repack-2.1.jar`。这两个jar包对于理解Spring框架的工作原理以及它们在实际应用中的作用至关重要。 首先,`spring-...

    开发工具 spring-core-4.3.6.RELEASE

    开发工具 spring-core-4.3.6.RELEASE开发工具 spring-core-4.3.6.RELEASE开发工具 spring-core-4.3.6.RELEASE开发工具 spring-core-4.3.6.RELEASE开发工具 spring-core-4.3.6.RELEASE开发工具 spring-core-4.3.6....

    seata-spring-boot-starter-1.3.0-API文档-中文版.zip

    赠送jar包:seata-spring-boot-starter-1.3.0.jar; 赠送原API文档:seata-spring-boot-starter-1.3.0-javadoc.jar; 赠送源代码:seata-spring-boot-starter-1.3.0-sources.jar; 赠送Maven依赖信息文件:seata-...

    spring-plugin-core-2.0.0.RELEASE-API文档-中文版.zip

    赠送jar包:spring-plugin-core-2.0.0.RELEASE.jar; 赠送原API文档:spring-plugin-core-2.0.0.RELEASE-javadoc.jar; 赠送源代码:spring-plugin-core-2.0.0.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring...

    spring-security-crypto-5.5.2-API文档-中文版.zip

    赠送jar包:spring-security-crypto-5.5.2.jar; 赠送原API文档:spring-security-crypto-5.5.2-javadoc.jar; 赠送源代码:spring-security-crypto-5.5.2-sources.jar; 赠送Maven依赖信息文件:spring-security-...

    spring-data-commons-2.0.6.RELEASE-API文档-中文版.zip

    赠送jar包:spring-data-commons-2.0.6.RELEASE.jar; 赠送原API文档:spring-data-commons-2.0.6.RELEASE-javadoc.jar; 赠送源代码:spring-data-commons-2.0.6.RELEASE-sources.jar; 赠送Maven依赖信息文件:...

    spring-security-crypto-5.6.1-API文档-中文版.zip

    赠送jar包:spring-security-crypto-5.6.1.jar; 赠送原API文档:spring-security-crypto-5.6.1-javadoc.jar; 赠送源代码:spring-security-crypto-5.6.1-sources.jar; 赠送Maven依赖信息文件:spring-security-...

    spring-session-data-redis-2.0.4.RELEASE-API文档-中英对照版.zip

    赠送jar包:spring-session-data-redis-2.0.4.RELEASE.jar; 赠送原API文档:spring-session-data-redis-2.0.4.RELEASE-javadoc.jar; 赠送源代码:spring-session-data-redis-2.0.4.RELEASE-sources.jar; 赠送...

    spring-jdbc-5.3.15-API文档-中文版.zip

    赠送jar包:spring-jdbc-5.3.15.jar; 赠送原API文档:spring-jdbc-5.3.15-javadoc.jar; 赠送源代码:spring-jdbc-5.3.15-sources.jar; 赠送Maven依赖信息文件:spring-jdbc-5.3.15.pom; 包含翻译后的API文档:...

    spring-security-core-5.3.9.RELEASE-API文档-中文版.zip

    赠送jar包:spring-security-core-5.3.9.RELEASE.jar; 赠送原API文档:spring-security-core-5.3.9.RELEASE-javadoc.jar; 赠送源代码:spring-security-core-5.3.9.RELEASE-sources.jar; 赠送Maven依赖信息文件:...

    springfox-spring-webflux-3.0.0-API文档-中文版.zip

    赠送jar包:springfox-spring-webflux-3.0.0.jar; 赠送原API文档:springfox-spring-webflux-3.0.0-javadoc.jar; 赠送源代码:springfox-spring-webflux-3.0.0-sources.jar; 赠送Maven依赖信息文件:springfox-...

    spring-context-support-1.0.10-API文档-中文版.zip

    赠送jar包:spring-context-support-1.0.10.jar; 赠送原API文档:spring-context-support-1.0.10-javadoc.jar; 赠送源代码:spring-context-support-1.0.10-sources.jar; 赠送Maven依赖信息文件:spring-context-...

    spring-security-oauth2-2.3.5.RELEASE-API文档-中文版.zip

    赠送jar包:spring-security-oauth2-2.3.5.RELEASE.jar; 赠送原API文档:spring-security-oauth2-2.3.5.RELEASE-javadoc.jar; 赠送源代码:spring-security-oauth2-2.3.5.RELEASE-sources.jar; 赠送Maven依赖信息...

Global site tag (gtag.js) - Google Analytics