Procházet zdrojové kódy

原始注解的使用案例

zhang před 3 roky
rodič
revize
039f8ece86

+ 2 - 0
src/main/java/Main.java

@@ -1,3 +1,4 @@
1
+import com.Config.JdbcData;
1 2
 import com.alibaba.druid.pool.DruidDataSource;
2 3
 import com.alibaba.druid.pool.DruidPooledConnection;
3 4
 import com.controller.UserController;
@@ -20,6 +21,7 @@ public class Main {
20 21
         UserService bean = applicationContext.getBean(UserServiceImpl.class);
21 22
 //        DruidDataSource dataSource = (DruidDataSource) beanFactory.getBean("dataSource");
22 23
         DruidDataSource dataSource = beanFactory.getBean(DruidDataSource.class);
24
+ //       JdbcData bean1 = beanFactory.getBean(JdbcData.class);
23 25
         DruidPooledConnection connection = dataSource.getConnection();
24 26
         connection.close();
25 27
         // userService1 在 xml文件中定义

+ 12 - 0
src/main/java/StudentMain.java

@@ -0,0 +1,12 @@
1
+import com.service.StudentService;
2
+import com.service.impl.StudentServiceImpl;
3
+import org.springframework.context.ApplicationContext;
4
+import org.springframework.context.support.ClassPathXmlApplicationContext;
5
+
6
+public class StudentMain {
7
+    public static void main(String[] args) {
8
+        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("context.xml");
9
+        StudentService service = applicationContext.getBean(StudentServiceImpl.class);
10
+        service.showStudent();
11
+    }
12
+}

+ 10 - 0
src/main/java/com/Config/JdbcData.java

@@ -0,0 +1,10 @@
1
+package com.Config;
2
+
3
+import org.springframework.beans.factory.annotation.Value;
4
+import org.springframework.stereotype.Component;
5
+
6
+@Component
7
+public class JdbcData {
8
+    @Value("${jdbc.url}")
9
+    private String url;
10
+}

+ 9 - 0
src/main/java/com/controller/UserController.java

@@ -1,7 +1,16 @@
1 1
 package com.controller;
2 2
 
3
+import com.Config.JdbcData;
4
+import org.springframework.beans.factory.annotation.Autowired;
5
+import org.springframework.beans.factory.annotation.Value;
3 6
 import org.springframework.stereotype.Controller;
4 7
 
5 8
 @Controller
6 9
 public class UserController {
10
+    @Autowired
11
+    private JdbcData jdbcData;
12
+    @Value("zxy")
13
+    private String name;
14
+    @Value("1971")
15
+    private Integer age;
7 16
 }

+ 5 - 0
src/main/java/com/dao/StudentDao.java

@@ -0,0 +1,5 @@
1
+package com.dao;
2
+
3
+public interface StudentDao {
4
+    public void showStudent();
5
+}

+ 13 - 0
src/main/java/com/dao/imp/StudentDaoImpl.java

@@ -0,0 +1,13 @@
1
+package com.dao.imp;
2
+
3
+import com.dao.StudentDao;
4
+import org.springframework.stereotype.Repository;
5
+
6
+//<bean id="studentDao" class="com.dao.imp.StudentDaoImpl"></bean>
7
+@Repository
8
+public class StudentDaoImpl implements StudentDao {
9
+    @Override
10
+    public void showStudent() {
11
+        System.out.println("show...");
12
+    }
13
+}

+ 5 - 0
src/main/java/com/service/StudentService.java

@@ -0,0 +1,5 @@
1
+package com.service;
2
+
3
+public interface StudentService {
4
+    public void showStudent();
5
+}

+ 24 - 0
src/main/java/com/service/impl/StudentServiceImpl.java

@@ -0,0 +1,24 @@
1
+package com.service.impl;
2
+
3
+import com.dao.StudentDao;
4
+import com.service.StudentService;
5
+import org.springframework.beans.factory.annotation.Autowired;
6
+import org.springframework.beans.factory.annotation.Qualifier;
7
+import org.springframework.stereotype.Service;
8
+
9
+import javax.annotation.Resource;
10
+
11
+//<bean id="studentService" class="com.service.impl.StudentServiceImpl">
12
+@Service
13
+public class StudentServiceImpl implements StudentService {
14
+//    <property name="studentDao" ref="studentDao"></property>
15
+//    @Autowired
16
+//    @Qualifier("studentDao") // 按照id名称值从容器中进行匹配 配合autowired一起使用
17
+//    @Resource(name = "studentDao") // resource相当于 autowired+qualifier
18
+    @Autowired
19
+    private StudentDao studentDao;
20
+    @Override
21
+    public void showStudent() {
22
+        studentDao.showStudent();
23
+    }
24
+}

+ 1 - 0
src/main/java/com/service/impl/UserServiceImpl.java

@@ -5,4 +5,5 @@ import org.springframework.stereotype.Service;
5 5
 @Service("UserServiceImpl")
6 6
 public class UserServiceImpl implements UserService {
7 7
 
8
+
8 9
 }

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

@@ -18,5 +18,6 @@
18 18
         <property name="username" value="${jdbc.username}"></property>
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 23
 </beans>

+ 13 - 0
src/test/java/CommonTest1.java

@@ -0,0 +1,13 @@
1
+import com.Config.JdbcData;
2
+import org.junit.Test;
3
+import org.springframework.beans.factory.annotation.Autowired;
4
+import org.springframework.beans.factory.annotation.Value;
5
+
6
+public class CommonTest1 {
7
+    @Autowired
8
+    JdbcData jdbcData;
9
+    @Test
10
+    public void testValue() {
11
+        System.out.println(jdbcData);
12
+    }
13
+}

+ 3 - 0
src/test/java/DataSourceTest.java

@@ -2,6 +2,7 @@ import com.alibaba.druid.pool.DruidDataSource;
2 2
 import com.alibaba.druid.pool.DruidPooledConnection;
3 3
 import com.mchange.v2.c3p0.ComboPooledDataSource;
4 4
 import org.junit.Test;
5
+import org.springframework.beans.factory.annotation.Value;
5 6
 
6 7
 import java.sql.Connection;
7 8
 import java.util.ResourceBundle;
@@ -14,6 +15,8 @@ public class DataSourceTest {
14 15
      * 测试c3p0数据源
15 16
      * @throws Exception
16 17
      */
18
+
19
+
17 20
     @Test
18 21
     public void c3p0Test() throws Exception{
19 22
         ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();