String框架搭建的基本步骤,及从 IOC & DI 容器中获取 Bean(spring框架bean的配置)--有实现数据库连接池的链接
Spring框架的插件springsource-tool-suite-3.4.0.RELEASE-e4.3.1-updatesite(是一个压缩包)导入步骤:
eclipse->help->Instal New Software->点击add,找到该压缩包->选择 Name下面选择带有 /Spring IDC 的多选框->取消最下边的选框 contact all...->next...->finish;
建立一个java project,在工程目录下(day-1)建立Folder的lib文件夹,放入Spring的五个jar架包放在里面,并且解压:
commons-logging-1.1.1.jar
spring-beans-4.0.0.RELEASE.jar
spring-context-4.0.0.RELEASE.jar
spring-core-4.0.0.RELEASE.jar
spring-expression-4.0.0.RELEASE.jar
上述步骤完毕后,就建立Spring的配置文件,一个典型的Spring项目,需要建立一个或多个bean的配置文件,建立Spring配置文件的步骤如下:
该工程下的src->other->spring->Spring Bean Configuration File;该文件的后缀为.xml;然后就建立包,java类...
-------------------------------------------------------------------------------------------------------------
以下是在Spring的IOC容器中配置Bean,ApplicationContext 是面向使用 Spring 框架的开发者,几乎所有的应用场合都直接使用ApplicationContext;
ApplicationContext 的主要实现类,ClassPathXmlApplicationContext:从 类路径下加载配置文件;ApplicationContext 在初始化上下文时就实例化所有单例的 Bean。
Spring的命名空间的使用(在该配置文件中,类似于标签)如下:
-----------------------------------------------------------------------------------
1.applicationContext.xml的配置文件:基本命名空间bean的使用 及 含有特殊标签<>的使用<![CDATA[<pnapan^-^>]]>
<bean id="helloWorld" class="com.atguigu.spring.beans.Hello">
<property name="name" value="spring"></property> </bean> <bean id="car" class="com.atguigu.spring.beans.Car">
//此处<constructor-arg value=" "后可添加 类型和索引 type="double" 或 index=”2“>
<constructor-arg value="4234534.21"></constructor-arg>
<constructor-arg value="red"></constructor-arg>
<constructor-arg value="baoma"></constructor-arg>
</bean> <bean id="person" class="com.atguigu.spring.beans.Person">
<property name="name">
<value><![CDATA[<pnapan^-^>]]></value>
</property>
<property name="age" value="24"></property>
<property name="car" ref="car"></property>
</bean>
src 目录下的bean方法主要是set方法,有Person类,Car类,Hello类他们都雷同,写一个就行了;
package com.atguigu.spring.beans;
public class Car {
private double price;
private String color;
private String name;
public Car(double price, String color, String name) {
super();
this.price = price;
this.color = color;
this.name = name;
}
@Override
public String toString() {
return "Car [price=" + price + ", color=" + color + ", name=" + name
+ "]";
}
}
在src目录下建立的main方法
package com.atguigu.spring.beans; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main {
public static void main(String[] args) {
ApplicationContext cxt=new ClassPathXmlApplicationContext("applicationContext.xml"); Hello hello=(Hello) cxt.getBean("helloWorld");
hello.hello();
Car car=(Car) cxt.getBean("car");
System.out.println(car); Person person=(Person) cxt.getBean("person");
System.out.println(person);
}
}
-----------------------------------------------------------------------------------
2.在配置文件中插入list和map集合,utli和 p 命名空间的使用,util适用于list集合,p简化代码;
其中person类中private list<Car> car; car属性是list集合泛型是Car;
person2类中private Map<Strng , Car> car;
DataSource类中只有private Properties properties;属性为文件类型,再设置get,set ,toStrong这类bean方法
<bean id="car" class="com.atguigu.spring.beans2.Car">
<property name="name" value="changan"></property>
<property name="color" value="red"></property>
<property name="price" value="2312321"></property>
</bean> <bean id="car2" class="com.atguigu.spring.beans2.Car">
<property name="name" value="baoma"></property>
<property name="color" value="green"></property>
<property name="price" value="890"></property>
</bean> <bean id="person" class="com.atguigu.spring.beans2.Person">
<property name="name" value="panpan"></property>
<property name="age" value="21"></property>
<property name="car">
<list>
<ref bean="car"/>
<ref bean="car2"/>
</list>
</property>
</bean> <bean id="person2" class="com.atguigu.spring.beans2.Person">
<property name="name" value="jiajia"></property>
<property name="age" value="22"></property>
<property name="car">
<map>
<entry key="AAA" value-ref="car"></entry>
<entry key="BBB" value-ref="car2"></entry> </map>
</property>
</bean> <bean id="properties" class="com.atguigu.spring.beans2.DataSource">
<property name="properties">
<props>
<prop key="username">oraclejava</prop>
<prop key="password">lxn123</prop>
<prop key="jdbcUrl">oracle</prop>
<prop key="driverClass">drivaer</prop>
</props>
</property> </bean> <util:list id="cars">
<ref bean="car"/>
<ref bean="car2"/>
</util:list> <bean id="person3" class="com.atguigu.spring.beans2.Person">
<property name="name" value="panpan"></property>
<property name="age" value="12"></property>
<property name="car">
<ref bean="cars"/>
</property>
</bean>
<bean id="person4" class="com.atguigu.spring.beans2.Person"
p:name="quanquan" p:age="23" p:car-ref="cars">
</bean>
Main类中的测试方法:
ApplicationContext app=new ClassPathXmlApplicationContext("applicationContext2.xml");
Person person=(Person) app.getBean("person");
System.out.println(person);
// Person person2=(Person) app.getBean("person2");
// System.out.println(person2);
DataSource dataSource=app.getBean(DataSource.class);
System.out.println(dataSource);
person=(Person) app.getBean("person3");
System.out.println(person);
person=(Person) app.getBean("person4");
System.out.println(person);
--------------------------------------------------------------------------------------
bean的自动装配:使用时必须保证全类名下的i d名和 type具有唯一性;
通过类型:autowire="byType";
通过id名:autowire="byName"
<bean id="person" class="com.atguigu.spring.beans3.Person"
p:name="panpan" p:car-ref="car" p:address-ref="address">
</bean>
<bean id="person2" class="com.atguigu.spring.beans3.Person"
p:name="jiajia" autowire="byName">
</bean>
<bean id="person3" class="com.atguigu.spring.beans3.Person"
p:name="sjkdls" autowire="byType">
</bean>
---------------------------------------------------------------------------------
bean之间的关系:依赖和继承
继承:父类:abstract 必须设为 true,子类:有parent=”“;
依赖:通过depends-on 属性设定....
<bean id="address"
p:city="nanjing" p:street="jianjian" abstract="true">
</bean>
<bean id="address2" class="com.atguigu.spring.beans3.Address"
p:street="panpan" parent="address" >
</bean>
-----------------------------------------------------------------------------
通过bean的配置读取文件中数据,实现c3p0数据库连接池的链接
src目录下建立file文件jdbc.properties
user=root
password=lxn123
driverclass=com.mysql.jdbc.Driver
jdbcurl=jdbc:mysql:///test
spring 的bean文件applicationContext4.xml中进行配置;这里使用到了context命名空间
<!-- 使用外部的配置文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${user}"></property>
<property name="password" value="${password}"></property>
<property name="driverClass" value="${driverclass}"></property>
<property name="jdbcUrl" value="${jdbcurl}"></property>
</bean>
Main类中测试:
ApplicationContext cx=new ClassPathXmlApplicationContext("applicationContext4.xml");
DataSource dataSource=(DataSource) cx.getBean("dataSource");
System.out.println(dataSource.getConnection());
String框架搭建的基本步骤,及从 IOC & DI 容器中获取 Bean(spring框架bean的配置)--有实现数据库连接池的链接的更多相关文章
- ssh框架搭建的基本步骤(以及各部分作用)
ssh框架搭建的基本步骤(以及各部分作用) 一.首先,明确spring,struts,hibernate在环境中各自的作用. struts: 用来响应用户的action,对应到相应的类进行 ...
- Unity 游戏框架搭建 2019 (四十八/四十九) MonoBehaviourSimplify 中的消息策略完善&关于发送事件的简单封装
MonoBehaviourSimplify 中的消息策略完善 在上一篇,笔者说,MonoBehaviourSimplify 中的消息策略还有一些小问题.我们在这篇试着解决一下. 先贴出来代码: usi ...
- Unity 游戏框架搭建 2019 (五十六/五十七) 需求分析-架构中最重要的一环&从 EmptyGO 到 Manager Of Managers
我们的项目开始立项的时候,最常见的一个情况就是:几个人的小团队,一开始什么也不做,就开始写代码,验证逻辑,游戏就开始写起来了.而公司的一些所谓的领导层面一开始就把游戏定义为我们要做一个大作.这个事情本 ...
- IOC和DI的概念,以及Spring框架的介绍
对于Java开发者来说,Spring肯定是一个避不开的技术.所以准备系统的学下Spring框架. 我给自己设计的学习路线是这样的:首先阅读下Spring的官方文档(注意Spring官网上有很多项目,S ...
- vue框架搭建的详细步骤(一)
在这里我们先快速的搭建一个vue的脚手架: (1).在安装vue的环境之前,安装NodeJS环境是必须的.可以使用node -v指令检查,需要保证安装了4.0版本以上的nodeJS环境. 没有安装的话 ...
- 使用VUE框架搭建项目基本步骤
ps:初入Vue坑的小伙伴们,对于独立做一个项目可能不清楚需要使用哪些资源,这篇随笔希望对大家有所帮助. 第一步:参照vue的官方文档,建立一个vue的项目 # 全局安装 vue-cli $ npm ...
- vue框架搭建的详细步骤之项目结构(二)
上一篇中简单的创建了一个脚手架,这篇简单的讲一下脚手架的项目结构: (1).build/ 此目录包含开发服务器和生产webpack构建的实际配置.通常,您不需要触摸这些文件,除非您要自定义We ...
- Spring框架之AOP源码完全解析
Spring框架之AOP源码完全解析 Spring可以说是Java企业开发里最重要的技术.Spring两大核心IOC(Inversion of Control控制反转)和AOP(Aspect Orie ...
- 跟着刚哥学习Spring框架--创建HelloWorld项目(一)
1.Spring框架简介 Spring是一个开源框架,Spring是在2003年兴起的一个轻量级的开源框架,由Rod johnson创建.主要对JavaBean的生命周期进行管理的轻量级框架,Spri ...
随机推荐
- linux:磁盘的分割、检验、格式化与挂载
新增一颗磁碟: 1.对磁碟进行分割,以建立可用的partition 2.对该分割槽partition进行格式化(format),以建立系统可用的filesystem 3.若要仔细点,可对刚刚建立的fi ...
- Effective C++ 5.实现
//条款26:尽量延后变量的定义式出现的时间 // 1.不仅应该延后变量的定义,更应该直到使用该变量的前一刻为止,甚至应该尝试延后这份定义直到能够给它初始值为止.如果这样,不仅能够避免构造和析构的非必 ...
- views of postgresql user password and encrypted or unencrypted
password_encryption = onpostgres=# create user user1 with encrypted password 'user1';CREATE ROLEpost ...
- Leetcode: Remove K Digits
Given a non-negative integer num represented as a string, remove k digits from the number so that th ...
- For嵌套输出图形
/*输出此图形 * * * * * * * * * ** * * * * * * * * * * * * * *解析:可以把此图形看成两部分----*---* *--* ...
- Java SE series:2. enhance your java basis! [doc chm: jdk6api Chinese reference]
1. javaee(Web) and Android 2. how to use eclipse and break point debuging in eclipse, as to java web ...
- [原创]java WEB学习笔记67:Struts2 学习之路-- 类型转换概述, 类型转换错误修改,如何自定义类型转换器
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- Java之Structs框架初探
今天是小白第一次自己的接触Struts框架,因为网上的资料都是从Structs2开始,跟Structs1完全不同,因此,小白直接跳过1学习版本2的搭建,废话不多说,直接开始. 首先要搭建框架,就肯定要 ...
- php CI框架目录结构及运行机制
CI目录结构 CI主要组成部分为,application(应用文件夹).system(系统文件夹)和index.php入口文件. 应用文件夹中主要是存放控制器.模型和视图等,系统文件夹中主 ...
- PAT乙级 1031. 查验身份证(15) 标志要清零!!!!!!!!!
1031. 查验身份证(15) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 一个合法的身份证号码由17位地区. ...