Spring框架今日份练习

第一个配置文件

<?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">
  <!-- 根据属性的set方法赋值 -->
   <bean id="hello" class="spring.hello.HelloModel"> 
   <property name="username" value="张三"></property>
   <property name="age" value="18"></property>
   </bean>
   <!-- 构造器赋值 -->
   <bean id="hello1" class="spring.hello.HelloModel">
   <constructor-arg name="name" value="12"></constructor-arg>
   <constructor-arg name="age" value="13"></constructor-arg>
   </bean>
   
   <!-- 构造器赋值,不指定属性name的话就要严格按照参数的位置,也可以根据索引idnex index是从0开始-->
   <bean id="hello2" class="spring.hello.HelloModel">
   <constructor-arg value="14" index="0"></constructor-arg>
   <constructor-arg value="15" index="1"></constructor-arg>
   </bean>
   <!-- 若构造方法有重载,那么可以指定参数的类型,type,写类全民 -->
   <bean id="hello2" class="spring.hello.HelloModel">
   <constructor-arg value="14" index="0" type="java.lang.String"></constructor-arg>
   <constructor-arg value="15" index="1" type="java.lang.String"></constructor-arg>
   </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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans.xsd">


	<!-- 静态工厂 (不需要创建工厂本身) factory-method="getAir"指定那个方法是工厂方法 class:指定静态工厂全类名 
		factory-method:指定工厂方法 <constructor-arg 若需要传参则用构造方法传参 -->
	<bean id="air" class="spring.airplan.StaticFacrtoy"
		factory-method="getAir">
		<constructor-arg name="name" value="张三"></constructor-arg>
	</bean>
	<!-- 实例工厂 需要先创建一个实例工厂 在创建一个对象,指定实例工厂factory-bean="Newfacrtoy" 指定工厂方法factory-method="getAir" 
		若需要传值则用构造方法 <constructor-arg name="name" value="张三"></constructor-arg>传值 -->
	<bean id="Newfacrtoy" class="spring.airplan.Newfacrtoy"></bean>
	<bean id="air1" class="spring.airplan.Air"
		factory-bean="Newfacrtoy" factory-method="getAir">
		<constructor-arg name="name" value="张三"></constructor-arg>
	</bean>

	<!-- 单实例 scope="singleto"默认是单实例 多实例设置属性scope="prototype" -->
	<bean id="car" class="spring.hello.Car" scope="prototype">
		<property name="name" value="baoma"></property>
		<property name="price" value="10000"></property>
	</bean>
	<!-- 单例bean的生命周期: (容器启动)构造器---init-method初始化方法-----destroy-method(容器关闭)销毁方法 
		 
	 -->
	 <bean id="danli" class="spring.hello.Car" init-method="init" destroy-method="destroy"></bean>
	 <!-- 
		   多例bean的生命周期: 获取bean(构造器)------init初始方法--------destroy容器关闭 不会调用bean的销毁方法
	 -->
	  <bean id="danli" class="spring.hello.Car" init-method="init" destroy-method="destroy" scope="prototypy"></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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans.xsd">

	<bean id="car01" class="spring.hello.Car">
		<property name="name" value="baoma"></property>
		<property name="price" value="10000"></property>
	</bean>

	<bean id="hello" class="spring.hello.HelloModel">
		<property name="username" value="sda"></property>
		<property name="age" value="14"></property>
		<!-- 外部引用使用ref为对象赋值 ref里面填写的是外部bean的id -->
		<property name="car" ref="car01">
			<!-- 内部引用,为对象赋值 <bean class="spring.hello.Car"> <property name="name" 
				value="baoma"></property> <property name="price" value="10000"></property> 
				</bean> -->
		</property>
	</bean>
	<bean id="hello1" class="spring.hello.HelloModel">
		<property name="username" value="maozi"></property>
		<property name="car">
			<bean class="spring.hello.Car">
				<property name="name" value="宝马"></property>
				<property name="price" value="10000"></property>
			</bean>
		</property>
		<property name="list">
			<list>
				<bean class="spring.hello.Car">
					<property name="name" value="本田"></property>
					<property name="price" value="19999"></property>
				</bean>
				<ref bean="car01" />
			</list>
		</property>
	</bean>
	<bean id="hello2" class="spring.hello.HelloModel">
		<property name="set">
			<set>
				<bean class="spring.hello.Car">
					<property name="name" value="baoma"></property>
					<property name="price" value="10000"></property>
				</bean>
				<ref bean="car01" />
			</set>
		</property>
	</bean>
	<bean id="hello3" class="spring.hello.HelloModel">
		<property name="map">
			<map>
				<!-- 一个entry代表一个键值对 -->
				<entry key="key01" value-ref="car01"/>
				<entry key="key02" value="1"/>
				
				<entry key="key03">
				<bean class="spring.hello.Car">
				<property name="name" value="baoma"></property>
				<property name="price" value="10000"></property>
				</bean>
				</entry>
			</map>
		</property>
	</bean>
	<!-- 级联属性就是属性的属性 -->
	<bean id="hello4" class="spring.hello.HelloModel">
	<property name="car" ref="car01"></property>
	<property name="car.name" value="oo"></property>
	</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:jdbc.properties"></context:property-placeholder>

<bean id="datasource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="driverClass" value="${jdbc.driverClassName}"></property>
</bean>
</beans>

jdbc外部文件

jdbc.driverClassName=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/miduoduo?useSSL=false&serverTimezone=UTC&characterEncoding=utf-8
jdbc.username=root
jdbc.password=123456
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值