Selaa lähdekoodia

使用Spring 新注解 configuration componentScan PropertySource 等、、

zhang 2 vuotta sitten
vanhempi
commit
2b7e9a9d31

+ 15 - 0
spring_annotation/src/main/java/AnnotationMain.java

@@ -0,0 +1,15 @@
1
+import com.Config.SpringConfiguration;
2
+import com.service.StudentService;
3
+import org.springframework.context.ApplicationContext;
4
+import org.springframework.context.annotation.AnnotationConfigApplicationContext;
5
+
6
+/**
7
+ * 使用注解引入容器
8
+ */
9
+public class AnnotationMain {
10
+    public static void main(String[] args) {
11
+        ApplicationContext applicationContext = new AnnotationConfigApplicationContext(SpringConfiguration.class);
12
+        Object jdbcUrl = applicationContext.getBean("getJdbcUrl");
13
+        StudentService bean = applicationContext.getBean(StudentService.class);
14
+    }
15
+}

+ 3 - 2
spring_annotation/src/main/java/Main.java

@@ -23,9 +23,10 @@ public class Main {
23 23
         DruidPooledConnection connection = dataSource.getConnection();
24 24
         connection.close();
25 25
         // userService1 在 xml文件中定义
26
-         MqService mqService = applicationContext.getBean(MqServiceImpl.class);
27
-         mqService.pushRabbitMessage();
26
+       //  MqService mqService = applicationContext.getBean(MqServiceImpl.class);
27
+        // mqService.pushRabbitMessage();
28 28
 //         mqService.listenRabbitQueue();
29
+        Object testStringBean = beanFactory.getBean("testStringBean");
29 30
     }
30 31
 
31 32
 }

+ 25 - 0
spring_annotation/src/main/java/com/Config/SpringConfiguration.java

@@ -0,0 +1,25 @@
1
+package com.Config;
2
+
3
+import org.springframework.beans.factory.annotation.Value;
4
+import org.springframework.context.annotation.Bean;
5
+import org.springframework.context.annotation.ComponentScan;
6
+import org.springframework.context.annotation.Configuration;
7
+import org.springframework.context.annotation.PropertySource;
8
+
9
+/**
10
+ * spring 核心配置文件类
11
+ */
12
+@Configuration
13
+// 组件扫描
14
+//<context:component-scan base-package="com"></context:component-scan>
15
+@ComponentScan({"com"})
16
+@PropertySource("classpath:jdbc.properties")
17
+public class SpringConfiguration {
18
+    // 写一个dataSource
19
+    @Value("${jdbc.url}")
20
+    String jdbcUrl;
21
+    @Bean("getJdbcUrl")
22
+    public String getJdbcUrl() {
23
+        return jdbcUrl;
24
+    }
25
+}

+ 12 - 0
spring_annotation/src/main/java/com/Config/TestConfiguration.java

@@ -0,0 +1,12 @@
1
+package com.Config;
2
+
3
+import org.springframework.context.annotation.Bean;
4
+import org.springframework.context.annotation.Configuration;
5
+
6
+@Configuration
7
+public class TestConfiguration {
8
+    @Bean
9
+    public String testStringBean() {
10
+        return "String";
11
+    }
12
+}

+ 1 - 1
spring_annotation/src/main/java/com/service/impl/MqServiceImpl.java

@@ -7,7 +7,7 @@ import org.springframework.stereotype.Service;
7 7
 import javax.annotation.PostConstruct;
8 8
 import java.io.IOException;
9 9
 
10
-@Service("MqServiceImpl")
10
+//@Service("MqServiceImpl")
11 11
 public class MqServiceImpl implements MqService {
12 12
     ConnectionFactory connectionFactory;
13 13
     Connection connection;

+ 1 - 1
spring_annotation/src/main/resources/context.xml

@@ -19,5 +19,5 @@
19 19
         <property name="password" value="${jdbc.password}"></property>
20 20
     </bean>
21 21
     <!--    组件扫描-->
22
-    <context:component-scan base-package="com"></context:component-scan>
22
+<!--    <context:component-scan base-package="com"></context:component-scan>-->
23 23
 </beans>