zoukankan      html  css  js  c++  java
  • Spring阶段性学习总结(八)注解开发配置Bean

    1 package SpringAnnotation;
    2 
    3 import org.springframework.stereotype.Component;
    4 
    5 @Component("testObject")
    6 public class TestObject {
    7 
    8 }
     1 package SpringAnnotation.service;
     2 
     3 import SpringAnnotation.repository.UserRepository;
     4 import org.springframework.beans.factory.annotation.Autowired;
     5 import org.springframework.stereotype.Service;
     6 
     7 @Service("userService")
     8 public class UserService {
     9     @Autowired
    10     private UserRepository userRepository;
    11 
    12     public void add() {
    13         System.out.println("UserService add....");
    14         userRepository.save();
    15     }
    16 
    17 }
    1 package SpringAnnotation.repository;
    2 
    3 public interface UserRepository {
    4     public  void save();
    5 }
     1 package SpringAnnotation.repository;
     2 
     3 import org.springframework.stereotype.Repository;
     4 
     5 @Repository("userRepository")
     6 
     7 public class UserRepositoryImp implements UserRepository {
     8     @Override
     9     public void save() {
    10         System.out.println("UserRepository save.....");
    11     }
    12 }
     1 package SpringAnnotation.Controller;
     2 
     3 import SpringAnnotation.service.UserService;
     4 import org.springframework.beans.factory.annotation.Autowired;
     5 import org.springframework.stereotype.Controller;
     6 
     7 @Controller("userController")
     8 public class UserController {
     9     @Autowired
    10      UserService userService;
    11 
    12     public void extute(){
    13         System.out.println("UserController extute....");
    14         userService.add();
    15     }
    16 
    17 }
     1 package SpringAnnotation;
     2 
     3 import SpringAnnotation.Controller.UserController;
     4 import SpringAnnotation.repository.UserRepository;
     5 import SpringAnnotation.service.UserService;
     6 import org.springframework.context.ApplicationContext;
     7 import org.springframework.context.support.ClassPathXmlApplicationContext;
     8 
     9 public class Main {
    10     public static void main(String[] args) {
    11         ApplicationContext ctx = new ClassPathXmlApplicationContext("SpringAnnotation/beans-annotation.xml");
    12       /* TestObject testObject=(TestObject) ctx.getBean("testObject");
    13         System.out.println(testObject);*/
    14 
    15        /* UserRepository userRepository=(UserRepository) ctx.getBean("userRepository");
    16         System.out.println(userRepository);*/
    17 
    18         UserController userController = (UserController) ctx.getBean("userController");
    19         System.out.println(userController);
    20         userController.extute();
    21 /*
    22         UserService userService=(UserService) ctx.getBean("userService");
    23         System.out.println(userService);*/
    24 
    25     }
    26 }
     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4        xmlns:context="http://www.springframework.org/schema/context"
     5        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">
     6 
     7 
     8     <!--制定IOC容器扫描的类-->
     9     <!--resource-pattern可进行范围限制-->
    10     <!--context:exclude-filter子节点指定排除哪些指定表达式的组件-->
    11     <!--<context:include-filter 表示只包含表达式的组件,需要配置use-default-filters=false-->
    12     <!--type:assignable 是用类名进行追踪来处理后面expression的内容-->
    13     <context:component-scan
    14             base-package="SpringAnnotation"
    15     >
    16     </context:component-scan>
    17 
    18 </beans>
  • 相关阅读:
    Oracle Core 学习笔记二 Transactions 和 Consistency 说明
    Oracle AUTO_SPACE_ADVISOR_JOB 说明
    Windows 下 ftp 上传文件 脚本
    Oracle 11g 中 Direct path reads 特性 说明
    Linux 使用 wget 下载 Oracle 软件说明
    Oracle 10g read by other session 等待 说明
    Oracle 11g RAC INS06006 Passwordless SSH connectivity not set up between the following node(s) 解决方法
    SecureCRT 工具 上传下载数据 与 ASCII、Xmodem、Ymodem 、Zmodem 说明
    Oracle RAC root.sh 报错 Timed out waiting for the CRS stack to start 解决方法
    Oracle RESETLOGS 和 NORESETLOGS 区别说明
  • 原文地址:https://www.cnblogs.com/zhang188660586/p/11557369.html
Copyright © 2011-2022 走看看