spring 完结

This commit is contained in:
法然
2022-10-14 23:39:59 +08:00
parent b91ab7307e
commit 24bc4f24fe
265 changed files with 7316 additions and 0 deletions

View File

@@ -0,0 +1,9 @@
/**
* @author yinkanglong
* @version $Id: ${NAME}, v 0.1 ${YEAR}-${MONTH}-${DAY} ${TIME} yinkanglong Exp $
*/
public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
}
}

View File

@@ -0,0 +1,50 @@
<?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"
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">
<!--开启组件扫描
1. 如果扫描多个包,多个包之间使用逗号隔开
2. 扫描包的上层目录-->
<context:component-scan base-package="com.ykl"></context:component-scan>
<!--引入外部属性文件-->
<!-- <context:property-path path="classpath:jdbc.properties"/>-->
<!-- <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">-->
<!-- <property name="driverClassName" value="${prop.driverClass}"></property>-->
<!-- <property name="url" value="${prop.url}"></property>-->
<!-- <property name="username" value="${prop.username}"></property>-->
<!-- <property name="password" value="${prop.password}"></property>-->
<!-- </bean>-->
<!--引入数据库连接-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3310/user"></property>
<property name="username" value="root"></property>
<property name="password" value="123456"></property>
</bean>
<!--创建jdbctemplate对象-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<!--注入参数-->
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--引入事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--开启事务注解-->
<tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
</beans>

View File

@@ -0,0 +1,59 @@
<?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"
xmlns:tx="http://www.springframework.org/schema/tx" 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
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<!--开启组件扫描
1. 如果扫描多个包,多个包之间使用逗号隔开
2. 扫描包的上层目录-->
<context:component-scan base-package="com.ykl"></context:component-scan>
<!--引入外部属性文件-->
<!-- <context:property-path path="classpath:jdbc.properties"/>-->
<!-- <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">-->
<!-- <property name="driverClassName" value="${prop.driverClass}"></property>-->
<!-- <property name="url" value="${prop.url}"></property>-->
<!-- <property name="username" value="${prop.username}"></property>-->
<!-- <property name="password" value="${prop.password}"></property>-->
<!-- </bean>-->
<!--引入数据库连接-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3310/user"></property>
<property name="username" value="root"></property>
<property name="password" value="123456"></property>
</bean>
<!--创建jdbctemplate对象-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<!--注入参数-->
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--引入事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--配置通知-->
<tx:advice id="txadvice">
<!---->
<tx:attributes>
<tx:method name="pay"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="pt" expression="execution(* com.ykl.*(..)))"/>
<aop:advisor advice-ref="txadvice" pointcut-ref="pt"/>
</aop:config>
</beans>

View File

@@ -0,0 +1,55 @@
/**
* Alipay.com Inc.
* Copyright (c) 2004-2022 All Rights Reserved.
*/
package com.ykl.config;
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import javax.activation.DataSource;
/**
* @author yinkanglong
* @version : TxConfig, v 0.1 2022-10-11 21:52 yinkanglong Exp $
*/
@Configuration //配置类
@ComponentScan(basePackages = "com.ykl") //开启组件扫描
@EnableTransactionManagement //开启事务管理
public class TxConfig {
//创建数据库连接池
@Bean
public DruidDataSource getDruidDataSource(){
DruidDataSource druidDataSource = new DruidDataSource();
druidDataSource.setDriverClassName("com.mysql.jdbc.Driver");
druidDataSource.setUrl("jdbc:mysql://localhost:3310/user");
druidDataSource.setUsername("root");
druidDataSource.setPassword("123456");
return druidDataSource;
}
//创建jdbcTemplate。这个参数会根据类型在容器中找到指定的对象
@Bean
public JdbcTemplate getJdbcTemplate(DruidDataSource dataSource){
JdbcTemplate jdbcTemplate = new JdbcTemplate();
jdbcTemplate.setDataSource(dataSource);
return jdbcTemplate;
}
//创建事务管理器的对象
@Bean
public DataSourceTransactionManager getDataSourceTransactionManager(DruidDataSource dataSource){
DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();
transactionManager.setDataSource(dataSource);
return transactionManager;
}
}

View File

@@ -0,0 +1,16 @@
/**
* Alipay.com Inc.
* Copyright (c) 2004-2022 All Rights Reserved.
*/
package com.ykl.dao;
/**
* @author yinkanglong
* @version : AccountDao, v 0.1 2022-10-11 10:42 yinkanglong Exp $
*/
public interface AccountDao {
public void addMoney();
public void reduceMoney();
}

View File

@@ -0,0 +1,34 @@
/**
* Alipay.com Inc.
* Copyright (c) 2004-2022 All Rights Reserved.
*/
package com.ykl.dao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
/**
* @author yinkanglong
* @version : AccountDaoImpl, v 0.1 2022-10-11 10:42 yinkanglong Exp $
*/
@Repository
public class AccountDaoImpl implements AccountDao{
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public void addMoney() {
String sql = "update account set money=money+? where username=?";
jdbcTemplate.update(sql,100,"mary");
System.out.println("addmoney");
}
@Override
public void reduceMoney() {
String sql = "update account set money=money-? where username=?";
jdbcTemplate.update(sql,100,"lucy");
System.out.println("reducemoney");
}
}

View File

@@ -0,0 +1,72 @@
/**
* Alipay.com Inc.
* Copyright (c) 2004-2022 All Rights Reserved.
*/
package com.ykl.entity;
/**
* @author yinkanglong
* @version : Account, v 0.1 2022-10-11 10:42 yinkanglong Exp $
*/
public class Account {
private String id;
private String username;
private Integer money;
/**
* Getter method for property <tt>id</tt>.
*
* @return property value of id
*/
public String getId() {
return id;
}
/**
* Setter method for property <tt>counterType</tt>.
*
* @param id value to be assigned to property id
*/
public void setId(String id) {
this.id = id;
}
/**
* Getter method for property <tt>username</tt>.
*
* @return property value of username
*/
public String getUsername() {
return username;
}
/**
* Setter method for property <tt>counterType</tt>.
*
* @param username value to be assigned to property username
*/
public void setUsername(String username) {
this.username = username;
}
/**
* Getter method for property <tt>money</tt>.
*
* @return property value of money
*/
public Integer getMoney() {
return money;
}
/**
* Setter method for property <tt>counterType</tt>.
*
* @param money value to be assigned to property money
*/
public void setMoney(Integer money) {
this.money = money;
}
}

View File

@@ -0,0 +1,30 @@
/**
* Alipay.com Inc.
* Copyright (c) 2004-2022 All Rights Reserved.
*/
package com.ykl.service;
import com.ykl.dao.AccountDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.Transactional;
/**
* @author yinkanglong
* @version : AccountService, v 0.1 2022-10-11 10:42 yinkanglong Exp $
*/
@Service
@Transactional
public class AccountService {
@Autowired
private AccountDao accountDao;
public void pay(){
accountDao.reduceMoney();
// int i= 1/0;
accountDao.addMoney();
}
}

View File

@@ -0,0 +1,29 @@
/**
* Alipay.com Inc.
* Copyright (c) 2004-2022 All Rights Reserved.
*/
package com.ykl.test;
import com.ykl.service.AccountService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* @author yinkanglong
* @version : JTest4, v 0.1 2022-10-12 11:30 yinkanglong Exp $
*/
//@ExtendWith(SpingExtension.class)
@ContextConfiguration("classpath:bean01.xml")
public class JTest4 {
@Autowired
private AccountService accountService;
@Test
public void test01(){
accountService.pay();
}
}

View File

@@ -0,0 +1,28 @@
/**
* Alipay.com Inc.
* Copyright (c) 2004-2022 All Rights Reserved.
*/
package com.ykl.test;
import com.ykl.service.AccountService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* @author yinkanglong
* @version : JTest4, v 0.1 2022-10-12 11:30 yinkanglong Exp $
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:bean01.xml")
public class JTest5 {
@Autowired
private AccountService accountService;
@Test
public void test01(){
accountService.pay();
}
}

View File

@@ -0,0 +1,60 @@
/**
* Alipay.com Inc.
* Copyright (c) 2004-2022 All Rights Reserved.
*/
package com.ykl.test;
import com.ykl.config.TxConfig;
import com.ykl.entity.Account;
import com.ykl.service.AccountService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.GenericApplicationContext;
import sun.net.www.content.text.Generic;
import java.util.ArrayList;
import java.util.List;
/**
* @author yinkanglong
* @version : Test01, v 0.1 2022-10-08 16:09 yinkanglong Exp $
*/
public class Test01 {
@Test
public void testBatchAdd(){
ApplicationContext context = new ClassPathXmlApplicationContext("bean01.xml");
AccountService accountService = context.getBean("accountService",AccountService.class);
accountService.pay();
}
@Test
public void testBatchAdd3(){
ApplicationContext context = new AnnotationConfigApplicationContext(TxConfig.class);
AccountService accountService = context.getBean("accountService",AccountService.class);
System.out.println("start");
accountService.pay();
System.out.println("end");
}
//函数式风格创建对象,交给对象进行管理
@Test
public void testGenericApplicationContext(){
GenericApplicationContext context = new GenericApplicationContext();
context.refresh();
context.registerBean("user1",Account.class,()->new Account());
//根据类型获取bean跟@Autowire一样都是根据类型手动装载
Account account = context.getBean("com.ykl.entity.Account",Account.class);
System.out.println(account);
//根据名称获取bean(跟@Qualified一样根据对象的id进行装配)
Account account2 = context.getBean("user1",Account.class);
System.out.println(account);
}
}

View File

@@ -0,0 +1,21 @@
/**
* Alipay.com Inc.
* Copyright (c) 2004-2022 All Rights Reserved.
*/
package com.ykl.test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author yinkanglong
* @version : UserLog, v 0.1 2022-10-12 10:59 yinkanglong Exp $
*/
public class UserLog {
private static final Logger log = LoggerFactory.getLogger(UserLog.class);
public static void main(String[] args) {
log.info("hello world");
log.warn("hello log4j");
}
}

View File

@@ -0,0 +1,4 @@
prop.driverClass=com.mysql.jdbc.Driver
prop.url=jdbc:mysql://localhost:3306/user
prop.username=root
prop.password=123456

View File

@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration status="INFO">
<appenders>
<console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSSS} [%t] %-5level %logger{36} - %msg%n"/>
</console>
</appenders>
<loggers>
<root level="info">
<appender-ref ref="Console"/>
</root>
</loggers>
</configuration>