配置文件
<?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: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.xsd">
<bean name="user" class="cn.spring.test01.User" scope="singleton" lazy-init="default" init-method="initUser" destroy-method="distoryUser"></bean>
</beans>
测试代码 /*** * 对象创建 scope =singleton 默认值 单例模式 * scope=protoytpe 原形 不是单例模式 * 区别 protoytpe 只有使用时候才创建对象 * singleton 默认启动的就创建 并且只创建一个 * lazy-init 默认为false 不延迟创建 启动的时候就创建(只对singleton有效) * lazy-init true 在用到对象的时候 才创建对象 * init-method 初始化方法的调用 * destroy-method 销毁方法的调用(注意要调用destory方法) * * */
public static void main(String[] args) {
// TODO Auto-generated method stub
ClassPathXmlApplicationContext ac =new ClassPathXmlApplicationContext("cn/spring/test01/applicationContext.xml");
System.out.println("创建容器了....");
User user =(User) ac.getBean("user");
System.out.println(user);
ac.destroy();
}
bens的创建和scope模式有关 单利模式 下的懒加载 就是在需要的时候才创建对象 否则就是启动的时候就自动去创建对象 而且是唯一的单例对象 prototype模式是原形模式在需要的时候才创建对象
实体类对象
package cn.spring.test01;
public class User {
public int getId() {
return id;
}
public User(){
System.out.println("创建对象了...");
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
private int id;
private String name;
public void initUser(){
System.out.println("初始化======");
}
public void distoryUser(){
System.out.println("销毁对象=====");
}
}
----------------------------------------------IOC容器的创建----------------------------------------------------------- (1)通过构造函数创建 配置文件如下
<!-- 调用有参函数创建对象 -->
<bean id="user2" class="cn.spring.test01.User">
<constructor-arg value="1" index="0" type="int" name="id"></constructor-arg>
<constructor-arg value="jakec" index="1" type="java.lang.String" name="name"></constructor-arg>
</bean>
有参构造函数
```
public User(int id,String name){
System.out.println("调用有参构造函数"+id+"---"+name);
this.id=id;
this.name=name;
}
(2)通过set方法依赖注入,最常用的方式
<bean id="user" class="cn.spring.test02.User">
<property name="id" value="555"></property>
<property name="name" value="yunzhishang"></property>
</bean>
需要有对象属性的set方法
public void setId(int id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
(3)通过工厂模式
<!-- -工厂类实例方法创建对象 -->
<bean id="factory" class="cn.spring.test01.UserFactory"></bean>
<bean id="user3" factory-bean="factory" factory-method="getInstance"></bean>
工厂模式中创建方法getInstance
private User getInstance(){
return new User(100, "通过工厂模式调用实例创建对象");
}
(4)通过set方式创建的两种方式demo
<bean id="userDao" class="cn.spring.test02.UserDao"></bean>
<bean id="userService" class="cn.spring.test02.UserService">
<property name="userDao" ref="userDao"></property>
</bean>
<bean id="userAction" class="cn.spring.test02.UserAction">
<property name="userService" ref="userService"></property>
</bean>
//内部bena的用法
<bean id="userAction1" class="cn.spring.test02.UserAction">
<property name="userService">
<bean class="cn.spring.test02.UserService">
<property name="userDao">
<bean class="cn.spring.test02.UserDao"></bean>
</property>
</bean>
</property>
</bean>
(5)p标签的方式实现
<bean id="userDao" class="cn.spring.test02.UserDao"></bean>
<bean id="userService" class="cn.spring.test02.UserService" p:userDao-ref="userDao"></bean>
<bean id="userAction" class="cn.spring.test02.UserAction" p:userService-ref="userService"></bean>
(6)AOP面向切片的编程
面向切片其实是把核心方法和公共方法分开的一种模式
切片类:公共方法涵盖的一些需要动态植入的一些类
切入点:需要动态植入的方法
配置信息:
<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" //注意加入此命名
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.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 开启注解扫描 -->
<context:component-scan base-package="cn.spring.aop"></context:component-scan>
<!--开启AOP注解模式 -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans> ``` 切片类: ``` @Component //加入IOC容器 @Aspect //声明为切片类 public class Aop {
//切片表达式 表示 在UserDao下面的所有方法前都加入改方法
@Before("execution( * cn.spring.aop.UserDao.*(..))")
public void begin(){
System.out.println("核心代码提交开始...");
}
//切片表达式 表示 在UserDao下面的所有方法后都加入改方法
@After("execution( * cn.spring.aop.UserDao.*(..))")
public void end(){
System.out.println("核心代码提交完成...");
}
}
(7)aop的xml 的配置
bean.xml的配置
<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
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.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="UserDao" class="cn.spring.aopxml.UserDao"></bean>
<bean id="OrderDao" class="cn.spring.aopxml.OrderDao"></bean>
<bean id="aop" class="cn.spring.aopxml.Aop"></bean>
<aop:config>
<!-- 表示 对cn.spring.aopxml包下面的所有类 所有方法拦截 注意切片表达式是定位到方法-->
<aop:pointcut expression="execution(* cn.spring.aopxml.*.*(..))" id="pt"/>
<aop:aspect ref="aop">
<!-- 在方法之前插begin方法 -->
<aop:before method="begin" pointcut-ref="pt"/>
<!-- 在方法之后插入end方法 -->
<aop:after method="end" pointcut-ref="pt"/>
</aop:aspect>
</aop:config>
</beans>
(8)spring 的jdbc的方法
xml的配置如下
配置spring的dataSource的配置
<!----注意 dataSource的class是com.mchange.v2.c3p0.ComboPooledDataSource 需要引入c3p0->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql:///test"></property>
<property name="user" value="root"></property>
<property name="password" value="123456"></property>
<property name="initialPoolSize" value="3"></property>
<property name="maxPoolSize" value="10"></property>
<property name="maxStatements" value="100"></property>
<property name="acquireIncrement" value="2"></property>
</bean>
spring中的模板对象JdbcTemplate
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<bean id="userDao" class="cn.spring.jdbc.UserDao">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>
代码:
public class UserDao {
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
public int save(){
String sql="insert into trole(ID,TEXT) values(?,?)";
int res=-1;
res= jdbcTemplate.update(sql,UUID.randomUUID().toString(),"哈哈哥");
return res;
}
}
(9)spring中的事物操作
xml配置如下
<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql:///test"></property>
<property name="user" value="root"></property>
<property name="password" value="123456"></property>
<property name="initialPoolSize" value="3"></property>
<property name="maxPoolSize" value="10"></property>
<property name="maxStatements" value="100"></property>
<property name="acquireIncrement" value="2"></property>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<bean id="userDao" class="cn.spring.transtion.UserDao">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>
<bean id="userService" class="cn.spring.transtion.UserService">
<property name="userDao" ref="userDao"></property>
</bean>
<!-- -事物管理器类 -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- -配置事物增强 -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="get" read-only="true"/>
<tx:method name="find" read-only="true"/>
<tx:method name="*" read-only="false"/>
</tx:attributes>
</tx:advice>
<!-- -aop配置 表示拦截- -cn.spring.transtion.UserService.Save(..) 应用事物>
<aop:config>
<aop:pointcut expression="execution (* cn.spring.transtion.UserService.Save(..))" id="pt"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="pt"/>
</aop:config>
</beans>
核心代码
public class UserService { @Resource private UserDao userDao; public int Save(){ int res=-1; res=userDao.save();
return res;
}
}
public class UserDao {
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
@Transactional
public int save(){
String sql="insert into trole(ID,TEXT) values(?,?)";
int res=-1;
res= jdbcTemplate.update(sql,UUID.randomUUID().toString(),"哈哈哥yeyey");
return res;
}
}
(10)通过注解模式操作事物
<?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:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 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.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql:///test"></property>
<property name="user" value="root"></property>
<property name="password" value="123456"></property>
<property name="initialPoolSize" value="3"></property>
<property name="maxPoolSize" value="10"></property>
<property name="maxStatements" value="100"></property>
<property name="acquireIncrement" value="2"></property>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 事务管理器类 -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- -开启扫描 -->
<context:component-scan base-package="cn.spring.annotion"></context:component-scan>
<tx:annotation-driven transaction-manager="txManager"/> </beans>
UserDao层
@Repository public class UserDao {
@Resource
private JdbcTemplate jdbcTemplate;
public int save(){
String sql="insert into trole(ID,TEXT) values(?,?)";
int res=-1;
res= jdbcTemplate.update(sql,UUID.randomUUID().toString(),"哈哈哥yeyey");
return res;
}
}
UserService层
@Service public class UserService {
@Resource
private UserDao userDao;
@Transactional //加入事物
public int Save(){
int res=-1;
res=userDao.save();
return res;
}
}
Action层
public static void main(String[] args) {
ApplicationContext ac=new ClassPathXmlApplicationContext("cn/spring/annotion/bean.xml");
UserService service=(UserService) ac.getBean("userService");
int res= service.Save();
if(res!=-1)
System.out.println("保存成功");
else
System.out.println("保存失败");
}