注解配置模块
This commit is contained in:
parent
eab7564a30
commit
94c5aeec7e
1
.idea/compiler.xml
generated
1
.idea/compiler.xml
generated
@ -7,6 +7,7 @@
|
|||||||
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
|
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
|
||||||
<outputRelativeToContentRoot value="true" />
|
<outputRelativeToContentRoot value="true" />
|
||||||
<module name="springLearn202503" />
|
<module name="springLearn202503" />
|
||||||
|
<module name="springLearn20250422" />
|
||||||
</profile>
|
</profile>
|
||||||
</annotationProcessing>
|
</annotationProcessing>
|
||||||
</component>
|
</component>
|
||||||
|
2
.idea/encodings.xml
generated
2
.idea/encodings.xml
generated
@ -3,5 +3,7 @@
|
|||||||
<component name="Encoding">
|
<component name="Encoding">
|
||||||
<file url="file://$PROJECT_DIR$/springLearn202503/src/main/java" charset="UTF-8" />
|
<file url="file://$PROJECT_DIR$/springLearn202503/src/main/java" charset="UTF-8" />
|
||||||
<file url="file://$PROJECT_DIR$/springLearn202503/src/main/resources" charset="UTF-8" />
|
<file url="file://$PROJECT_DIR$/springLearn202503/src/main/resources" charset="UTF-8" />
|
||||||
|
<file url="file://$PROJECT_DIR$/springLearn20250422/src/main/java" charset="UTF-8" />
|
||||||
|
<file url="file://$PROJECT_DIR$/springLearn20250422/src/main/resources" charset="UTF-8" />
|
||||||
</component>
|
</component>
|
||||||
</project>
|
</project>
|
1
.idea/misc.xml
generated
1
.idea/misc.xml
generated
@ -5,6 +5,7 @@
|
|||||||
<option name="originalFiles">
|
<option name="originalFiles">
|
||||||
<list>
|
<list>
|
||||||
<option value="$PROJECT_DIR$/springLearn202503/pom.xml" />
|
<option value="$PROJECT_DIR$/springLearn202503/pom.xml" />
|
||||||
|
<option value="$PROJECT_DIR$/springLearn20250422/pom.xml" />
|
||||||
</list>
|
</list>
|
||||||
</option>
|
</option>
|
||||||
</component>
|
</component>
|
||||||
|
@ -4,6 +4,7 @@ import com.alibaba.druid.pool.DruidDataSource;
|
|||||||
import com.alibaba.druid.pool.DruidPooledConnection;
|
import com.alibaba.druid.pool.DruidPooledConnection;
|
||||||
import com.mchange.v2.c3p0.ComboPooledDataSource;
|
import com.mchange.v2.c3p0.ComboPooledDataSource;
|
||||||
import com.sec.dao.UserDao;
|
import com.sec.dao.UserDao;
|
||||||
|
import com.sec.service.UserService;
|
||||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||||
import org.springframework.context.support.FileSystemXmlApplicationContext;
|
import org.springframework.context.support.FileSystemXmlApplicationContext;
|
||||||
@ -21,7 +22,8 @@ public class Main {
|
|||||||
static String password = "";
|
static String password = "";
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
Main.getSpring();
|
// Main.getSpring();
|
||||||
|
Main.getSpring1();
|
||||||
Main.getSetting();
|
Main.getSetting();
|
||||||
Main.getDruid();
|
Main.getDruid();
|
||||||
Main.getC3p0();
|
Main.getC3p0();
|
||||||
@ -42,6 +44,12 @@ public class Main {
|
|||||||
// userDao.getData();
|
// userDao.getData();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void getSpring1(){
|
||||||
|
ClassPathXmlApplicationContext application=new ClassPathXmlApplicationContext("application.xml");
|
||||||
|
UserService userService= (UserService)application.getBean("UserService");
|
||||||
|
userService.getUser();
|
||||||
|
}
|
||||||
|
|
||||||
//读取properties文件 对数据库连接信息进行设置
|
//读取properties文件 对数据库连接信息进行设置
|
||||||
//目的是为了将配置信息与代码进行解耦
|
//目的是为了将配置信息与代码进行解耦
|
||||||
public static void getSetting(){
|
public static void getSetting(){
|
||||||
@ -52,6 +60,7 @@ public class Main {
|
|||||||
password=rb.getString("jdbc.password");
|
password=rb.getString("jdbc.password");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//数据库连接池
|
||||||
public static void getDruid(){
|
public static void getDruid(){
|
||||||
DruidDataSource druidDataSource=new DruidDataSource();
|
DruidDataSource druidDataSource=new DruidDataSource();
|
||||||
druidDataSource.setDriverClassName(driver);
|
druidDataSource.setDriverClassName(driver);
|
||||||
|
@ -0,0 +1,10 @@
|
|||||||
|
package com.sec.service;
|
||||||
|
|
||||||
|
import com.sec.dao.UserDao;
|
||||||
|
import com.sec.dao.impl.UserDaoImpl;
|
||||||
|
|
||||||
|
public interface UserService {
|
||||||
|
|
||||||
|
|
||||||
|
public void getUser();
|
||||||
|
}
|
@ -0,0 +1,20 @@
|
|||||||
|
package com.sec.service.impl;
|
||||||
|
|
||||||
|
import com.sec.dao.UserDao;
|
||||||
|
import com.sec.service.UserService;
|
||||||
|
|
||||||
|
public class UserServiceImpl implements UserService {
|
||||||
|
public UserDao userDao;
|
||||||
|
/* public void setUserDao(UserDao userDao){
|
||||||
|
this.userDao = userDao;
|
||||||
|
};*/
|
||||||
|
|
||||||
|
public UserServiceImpl(UserDao userDao) {
|
||||||
|
this.userDao = userDao;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void getUser() {
|
||||||
|
userDao.getData();
|
||||||
|
}
|
||||||
|
}
|
@ -3,7 +3,18 @@
|
|||||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
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">
|
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
|
||||||
<!-- <bean id="UserDao" class="com.impl.dao.com.sec.UserDaoImpl" scope="singleton" init-method="init" destroy-method="destroy"></bean>-->
|
<!-- <bean id="UserDao" class="com.impl.dao.com.sec.UserDaoImpl" scope="singleton" init-method="init" destroy-method="destroy"></bean>-->
|
||||||
<!-- <bean id="UserDao" class="com.factroy.com.sec.StaticFactroy" factory-method="getUserDao"></bean>-->
|
<!-- <bean id="UserDao" class="com.sec.factroy.StaticFactroy" factory-method="getUserDao"></bean>-->
|
||||||
<bean id="DemoFactroy" class="com.sec.factroy.DemoFactroy"></bean>
|
<!-- <bean id="DemoFactroy" class="com.sec.factroy.DemoFactroy"></bean>
|
||||||
<bean id="UserDao" factory-bean="DemoFactroy" factory-method="getDao"></bean>
|
<bean id="UserDao" factory-bean="DemoFactroy" factory-method="getDao"></bean>-->
|
||||||
|
<bean id="UserDao" class="com.sec.dao.impl.UserDaoImpl"></bean>
|
||||||
|
<!-- set方式进行依赖注入
|
||||||
|
<bean id="UserService" class="com.sec.service.impl.UserServiceImpl">
|
||||||
|
<property name="userDao" ref="UserDao"></property>
|
||||||
|
</bean>
|
||||||
|
-->
|
||||||
|
<!--构造方法进行依赖注入-->
|
||||||
|
<bean id="UserService" class="com.sec.service.impl.UserServiceImpl">
|
||||||
|
<constructor-arg name="userDao" ref="UserDao"></constructor-arg>
|
||||||
|
</bean>
|
||||||
|
|
||||||
</beans>
|
</beans>
|
@ -3,7 +3,13 @@
|
|||||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
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">
|
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
|
||||||
<!-- <bean id="UserDao" class="com.impl.dao.com.sec.UserDaoImpl" scope="singleton" init-method="init" destroy-method="destroy"></bean>-->
|
<!-- <bean id="UserDao" class="com.impl.dao.com.sec.UserDaoImpl" scope="singleton" init-method="init" destroy-method="destroy"></bean>-->
|
||||||
<!-- <bean id="UserDao" class="com.factroy.com.sec.StaticFactroy" factory-method="getUserDao"></bean>-->
|
<!-- <bean id="UserDao" class="com.sec.factroy.StaticFactroy" factory-method="getUserDao"></bean>-->
|
||||||
<bean id="DemoFactroy" class="com.sec.factroy.DemoFactroy"></bean>
|
<!-- <bean id="DemoFactroy" class="com.sec.factroy.DemoFactroy"></bean>
|
||||||
<bean id="UserDao" factory-bean="DemoFactroy" factory-method="getDao"></bean>
|
<bean id="UserDao" factory-bean="DemoFactroy" factory-method="getDao"></bean>-->
|
||||||
|
<bean id="UserDao" class="com.sec.dao.impl.UserDaoImpl"></bean>
|
||||||
|
<bean id="UserService" class="com.sec.service.impl.UserServiceImpl">
|
||||||
|
<!-- <property name="userDao" ref="UserDao"></property>-->
|
||||||
|
<constructor-arg name="userDao" ref="UserDao"></constructor-arg>
|
||||||
|
</bean>
|
||||||
|
|
||||||
</beans>
|
</beans>
|
Binary file not shown.
34
springLearn20250422/pom.xml
Normal file
34
springLearn20250422/pom.xml
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
<?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>com.sec</groupId>
|
||||||
|
<artifactId>springLearn20250422</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<maven.compiler.source>8</maven.compiler.source>
|
||||||
|
<maven.compiler.target>8</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.21</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>mysql</groupId>
|
||||||
|
<artifactId>mysql-connector-java</artifactId>
|
||||||
|
<version>8.0.23</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba</groupId>
|
||||||
|
<artifactId>druid</artifactId>
|
||||||
|
<version>1.1.22</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
</project>
|
@ -0,0 +1,34 @@
|
|||||||
|
package com.sec.config;
|
||||||
|
|
||||||
|
|
||||||
|
import com.alibaba.druid.pool.DruidDataSource;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.PropertySource;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* 在多个配置的情况下 配置文件根据不同功能进行拆分
|
||||||
|
* 在主配置中导入即可
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
@PropertySource("classpath:jdbc.properties")
|
||||||
|
public class JdbcConfig {
|
||||||
|
@Value("${jdbc.driver}")
|
||||||
|
private String driverClassName ;
|
||||||
|
@Value("${jdbc.url}")
|
||||||
|
private String jdbcUrl;
|
||||||
|
@Value("${jdbc.user}")
|
||||||
|
private String username ;
|
||||||
|
@Value("${jdbc.password}")
|
||||||
|
private String password;
|
||||||
|
|
||||||
|
@Bean("DruidDataSource")
|
||||||
|
public DruidDataSource getDruidDataSource() {
|
||||||
|
DruidDataSource druidDataSource = new DruidDataSource();
|
||||||
|
druidDataSource.setDriverClassName(driverClassName);
|
||||||
|
druidDataSource.setUrl(jdbcUrl);
|
||||||
|
druidDataSource.setUsername(username);
|
||||||
|
druidDataSource.setPassword(password);
|
||||||
|
return druidDataSource;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,19 @@
|
|||||||
|
package com.sec.config;
|
||||||
|
|
||||||
|
import com.alibaba.druid.pool.DruidDataSource;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.context.annotation.*;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* @Configuration 表明当前类是 配置类
|
||||||
|
* @ComponentScan扫描对应文件下的注解格式的bean进行注入
|
||||||
|
* @PropertySource加载对应的属性文件 用@Value将数据进行注入赋值
|
||||||
|
* @Value简单类型注入 直接@Value("要注入的数据")
|
||||||
|
* 将属性文件的变量数值进行注入 @Value("${要注入的数据变量名称}") 配合@PropertySource使用
|
||||||
|
* */
|
||||||
|
@Configuration
|
||||||
|
@ComponentScan("com.sec")
|
||||||
|
@Import(JdbcConfig.class)
|
||||||
|
public class LearnConfig {
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,5 @@
|
|||||||
|
package com.sec.dao;
|
||||||
|
|
||||||
|
public interface LearnDao {
|
||||||
|
public void getLearnData();
|
||||||
|
}
|
@ -0,0 +1,37 @@
|
|||||||
|
package com.sec.dao.impl;
|
||||||
|
|
||||||
|
import com.alibaba.druid.pool.DruidDataSource;
|
||||||
|
import com.alibaba.druid.pool.DruidPooledConnection;
|
||||||
|
import com.sec.dao.LearnDao;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
import java.sql.ResultSet;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.sql.Statement;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* @Component该类为spring管理的bean
|
||||||
|
* 其衍生了三种注解
|
||||||
|
* @Controller 表现层
|
||||||
|
* @Service 业务层
|
||||||
|
* @Repository 数据层
|
||||||
|
* */
|
||||||
|
@Repository
|
||||||
|
public class LearnDaoImpl implements LearnDao {
|
||||||
|
@Autowired
|
||||||
|
private DruidDataSource druidDataSource;
|
||||||
|
@Override
|
||||||
|
public void getLearnData() {
|
||||||
|
try {
|
||||||
|
DruidPooledConnection connection = druidDataSource.getConnection();
|
||||||
|
Statement statement = connection.createStatement();
|
||||||
|
ResultSet resultSet = statement.executeQuery("select * from user_info");
|
||||||
|
while (resultSet.next()) {
|
||||||
|
System.out.println(resultSet.getString("user_name"));
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,5 @@
|
|||||||
|
package com.sec.service;
|
||||||
|
|
||||||
|
public interface LearnService {
|
||||||
|
public void learn();
|
||||||
|
}
|
@ -0,0 +1,17 @@
|
|||||||
|
package com.sec.service.impl;
|
||||||
|
|
||||||
|
import com.sec.dao.LearnDao;
|
||||||
|
import com.sec.service.LearnService;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class LearnServiceImpl implements LearnService {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
public LearnDao learnDao;
|
||||||
|
@Override
|
||||||
|
public void learn() {
|
||||||
|
learnDao.getLearnData();
|
||||||
|
}
|
||||||
|
}
|
5
springLearn20250422/src/main/resources/application.xml
Normal file
5
springLearn20250422/src/main/resources/application.xml
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
<?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">
|
||||||
|
</beans>
|
4
springLearn20250422/src/main/resources/jdbc.properties
Normal file
4
springLearn20250422/src/main/resources/jdbc.properties
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
jdbc.driver=com.mysql.jdbc.Driver
|
||||||
|
jdbc.url=jdbc:mysql://47.103.155.22:3306/learn_spring
|
||||||
|
jdbc.user=root
|
||||||
|
jdbc.password=mysql_wafeYX
|
Loading…
x
Reference in New Issue
Block a user