5/22 12:20

This commit is contained in:
zyx 2023-05-22 12:20:36 +08:00
parent 68a8a9f33a
commit cd603a7d1d
23 changed files with 441 additions and 1 deletions

4
.idea/compiler.xml generated
View File

@ -8,11 +8,13 @@
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
<outputRelativeToContentRoot value="true" />
<module name="spring519" />
<module name="mybatis519" />
<module name="spring522-annotation" />
<module name="spring522" />
<module name="mybatis516" />
<module name="demo516" />
<module name="demo515" />
<module name="mybatis517-518" />
<module name="mybatis519" />
</profile>
</annotationProcessing>
</component>

4
.idea/encodings.xml generated
View File

@ -13,5 +13,9 @@
<file url="file://$PROJECT_DIR$/mybatis519/src/main/resources" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/spring519/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/spring519/src/main/resources" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/spring522-annotation/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/spring522-annotation/src/main/resources" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/spring522/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/spring522/src/main/resources" charset="UTF-8" />
</component>
</project>

2
.idea/misc.xml generated
View File

@ -14,6 +14,8 @@
<option value="$PROJECT_DIR$/mybatis517-518/pom.xml" />
<option value="$PROJECT_DIR$/mybatis519/pom.xml" />
<option value="$PROJECT_DIR$/spring519/pom.xml" />
<option value="$PROJECT_DIR$/spring522/pom.xml" />
<option value="$PROJECT_DIR$/spring522-annotation/pom.xml" />
</list>
</option>
</component>

View File

@ -0,0 +1,36 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.czyx007</groupId>
<artifactId>spring522-annotation</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.23</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,19 @@
package cn.czyx007.bean;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* @author : 张宇轩
* @createTime : 2023/5/22 - 9:34
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
private String id;
private String userName;
private String address;
private Integer age;
}

View File

@ -0,0 +1,12 @@
package cn.czyx007.dao;
import cn.czyx007.bean.User;
/**
* @author : 张宇轩
* @createTime : 2023/5/22 - 9:35
*/
public interface UserDao {
//根据id查询用户信息
User getUser(String id);
}

View File

@ -0,0 +1,19 @@
package cn.czyx007.dao.impl;
import cn.czyx007.bean.User;
import cn.czyx007.dao.UserDao;
import org.springframework.stereotype.Repository;
/**
* @author : 张宇轩
* @createTime : 2023/5/22 - 9:36
*/
@Repository(value = "mysqlUserDao")
public class MysqlUserDaoImpl implements UserDao {
@Override
public User getUser(String id) {
System.out.println("通过MySQl查询用户信息成功");
return null;
}
}

View File

@ -0,0 +1,18 @@
package cn.czyx007.dao.impl;
import cn.czyx007.bean.User;
import cn.czyx007.dao.UserDao;
import org.springframework.stereotype.Repository;
/**
* @author : 张宇轩
* @createTime : 2023/5/22 - 9:37
*/
@Repository(value = "oracleUserDao")
public class OracleUserDaoImpl implements UserDao {
@Override
public User getUser(String id) {
System.out.println("通过Oracle查询用户信息成功");
return null;
}
}

View File

@ -0,0 +1,11 @@
package cn.czyx007.service;
import cn.czyx007.bean.User;
/**
* @author : 张宇轩
* @createTime : 2023/5/22 - 9:40
*/
public interface UserService {
User findUserById(String id);
}

View File

@ -0,0 +1,27 @@
package cn.czyx007.service.impl;
import cn.czyx007.bean.User;
import cn.czyx007.dao.UserDao;
import cn.czyx007.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
/**
* @author : 张宇轩
* @createTime : 2023/5/22 - 9:41
*/
//若使用注解方式则不需要主动提供sette方法
@Service("userService")
public class UserServiceImpl implements UserService {
//自动注入
@Autowired
//指定使用哪一个dao
@Qualifier("mysqlUserDao")
UserDao userDao;
@Override
public User findUserById(String id) {
return userDao.getUser(id);
}
}

View File

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 开启注解扫描包 -->
<context:component-scan base-package="cn.czyx007.*"/>
</beans>

View File

@ -0,0 +1,19 @@
import cn.czyx007.bean.User;
import cn.czyx007.service.impl.UserServiceImpl;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @author : 张宇轩
* @createTime : 2023/5/22 - 11:44
*/
public class TestDemo {
@Test
public void test1(){
ApplicationContext app = new ClassPathXmlApplicationContext("spring.xml");
UserServiceImpl userService = app.getBean("userService", UserServiceImpl.class);
User user = userService.findUserById("1");
System.out.println("user = " + user);
}
}

35
spring522/pom.xml Normal file
View File

@ -0,0 +1,35 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.czyx007</groupId>
<artifactId>spring522</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.23</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,22 @@
package cn.czyx007.bean;
import lombok.AllArgsConstructor;
import lombok.Data;
/**
* @author : 张宇轩
* @createTime : 2023/5/19 - 15:59
*/
@Data
//@NoArgsConstructor
@AllArgsConstructor
public class Dog {
public Dog() {
System.out.println("狗出生了");
}
private String name;
public void eat(){
System.out.println(name + "的狗吃骨头...");
}
}

View File

@ -0,0 +1,19 @@
package cn.czyx007.bean;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* @author : 张宇轩
* @createTime : 2023/5/22 - 9:34
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
private String id;
private String userName;
private String address;
private Integer age;
}

View File

@ -0,0 +1,12 @@
package cn.czyx007.dao;
import cn.czyx007.bean.User;
/**
* @author : 张宇轩
* @createTime : 2023/5/22 - 9:35
*/
public interface UserDao {
//根据id查询用户信息
User getUser(String id);
}

View File

@ -0,0 +1,17 @@
package cn.czyx007.dao.impl;
import cn.czyx007.bean.User;
import cn.czyx007.dao.UserDao;
/**
* @author : 张宇轩
* @createTime : 2023/5/22 - 9:36
*/
public class MysqlUserDaoImpl implements UserDao {
@Override
public User getUser(String id) {
System.out.println("通过MySQl查询用户信息成功");
return null;
}
}

View File

@ -0,0 +1,16 @@
package cn.czyx007.dao.impl;
import cn.czyx007.bean.User;
import cn.czyx007.dao.UserDao;
/**
* @author : 张宇轩
* @createTime : 2023/5/22 - 9:37
*/
public class OracleUserDaoImpl implements UserDao {
@Override
public User getUser(String id) {
System.out.println("通过Oracle查询用户信息成功");
return null;
}
}

View File

@ -0,0 +1,11 @@
package cn.czyx007.service;
import cn.czyx007.bean.User;
/**
* @author : 张宇轩
* @createTime : 2023/5/22 - 9:40
*/
public interface UserService {
User findUserById(String id);
}

View File

@ -0,0 +1,24 @@
package cn.czyx007.service.impl;
import cn.czyx007.bean.User;
import cn.czyx007.dao.UserDao;
import cn.czyx007.service.UserService;
/**
* @author : 张宇轩
* @createTime : 2023/5/22 - 9:41
*/
public class MysqlUserServiceImpl implements UserService {
//高耦合换用spring解耦合
// UserDao userDao = new MysqlUserDaoImpl();
UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
@Override
public User findUserById(String id) {
return userDao.getUser(id);
}
}

View File

@ -0,0 +1,23 @@
package cn.czyx007.service.impl;
import cn.czyx007.bean.User;
import cn.czyx007.dao.UserDao;
import cn.czyx007.service.UserService;
/**
* @author : 张宇轩
* @createTime : 2023/5/22 - 9:41
*/
public class OracleUserServiceImpl implements UserService {
// UserDao userDao = new OracleUserDaoImpl();
UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
@Override
public User findUserById(String id) {
return userDao.getUser(id);
}
}

View File

@ -0,0 +1,36 @@
<?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">
<!--
scope=prototype 设置多例模式
默认单例
lazy-init="true" 懒加载
-->
<bean id="dog" class="cn.czyx007.bean.Dog" scope="prototype" lazy-init="true">
<!-- 依赖注入,对 对象属性赋值 -->
<!-- 需要提供setter方法因为该标签使用setter注入方式 -->
<property name="name" value="xxx"/>
</bean>
<!-- 把dao的实现交给spring管理-->
<bean id="mysqlUserDao" class="cn.czyx007.dao.impl.MysqlUserDaoImpl"/>
<bean id="oracleUserDao" class="cn.czyx007.dao.impl.OracleUserDaoImpl"/>
<!-- 把service的实现交给spring管理-->
<bean id="mysqlUserService" class="cn.czyx007.service.impl.UserServiceImpl">
<!--注入dao对象-->
<property name="userDao" ref="mysqlUserDao"/>
</bean>
<bean id="oracleUserService" class="cn.czyx007.service.impl.OracleUserServiceImpl">
<property name="userDao" ref="oracleUserDao"/>
</bean>
<!--通过构造器依赖注入-->
<bean id="user" class="cn.czyx007.bean.User">
<constructor-arg index="0" type="java.lang.String" name="id" value="1001"/>
<constructor-arg index="1" type="java.lang.String" name="userName" value="柳岩"/>
<constructor-arg index="2" type="java.lang.String" name="address" value="北京"/>
<constructor-arg index="3" type="java.lang.Integer" name="age" value="18"/>
</bean>
</beans>

View File

@ -0,0 +1,43 @@
import cn.czyx007.bean.Dog;
import cn.czyx007.bean.User;
import cn.czyx007.service.impl.OracleUserServiceImpl;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @author : 张宇轩
* @createTime : 2023/5/22 - 9:31
*/
public class TestDemo {
@Test
public void test1(){
ClassPathXmlApplicationContext app = new ClassPathXmlApplicationContext("spring.xml");
Dog dog = app.getBean("dog", Dog.class);
dog.eat();
Dog dog1 = app.getBean("dog", Dog.class);
System.out.println("是否是同一个对象 " + (dog == dog1));
}
//要注意交给spring管理后不能再自己new
@Test
public void test2(){
OracleUserServiceImpl userService = new OracleUserServiceImpl();
User user = userService.findUserById("1");
System.out.println("user = " + user);
}
@Test
public void test3(){
ClassPathXmlApplicationContext app = new ClassPathXmlApplicationContext("spring.xml");
OracleUserServiceImpl userService = app.getBean("oracleUserService", OracleUserServiceImpl.class);
User user = userService.findUserById("1");
System.out.println("user = " + user);
}
@Test
public void test4(){
ClassPathXmlApplicationContext app = new ClassPathXmlApplicationContext("spring.xml");
User user = app.getBean("user", User.class);
System.out.println("user = " + user);
}
}