zoukankan      html  css  js  c++  java
  • Spring学习--通过注解配置 Bean (一)

    在 classpath 中扫描组件:

    组件扫描(component scanning): Spring 能够从 classpath 下自动扫描 , 侦测和实例化具有特定注解的组件。

    特定组件包括:

    • @Component:基本注解 , 标识了一个受 Spring 管理的组件。
    • @Repository:标识持久层组件。
    • @Service:标识服务层(业务层)组件。
    • @Controller:标识表现层组件。

    对于扫描到的组件 , Spring 有默认的命名策略:使用非限定类名 , 第一个字母小写。也可以通过 value 属性值标识组件的名称。

     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
     6        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
     7 
     8     <!-- 指定 Spring IOC 容器扫描的包-->
     9     <context:component-scan base-package="com.itdjx.spring.annotation">
    10 
    11     </context:component-scan>
    12 
    13 </beans>

    注意:一定要添加 context 命名空间。

     1 package com.itdjx.spring.annotation;
     2 
     3 import org.springframework.stereotype.Component;
     4 
     5 /**
     6  * @author Wáng Chéng Dá
     7  * @create 2017-03-03 8:44
     8  */
     9 @Component
    10 public class TestObject {
    11 }
     1 package com.itdjx.spring.annotation.controller;
     2 
     3 import org.springframework.stereotype.Controller;
     4 
     5 /**
     6  * @author Wáng Chéng Dá
     7  * @create 2017-03-03 9:28
     8  */
     9 @Controller
    10 public class UserController {
    11 
    12     public void execute() {
    13         System.out.println("I am UserController's execute method...");
    14     }
    15 }
     1 package com.itdjx.spring.annotation.service;
     2 
     3 import org.springframework.stereotype.Service;
     4 
     5 /**
     6  * @author Wáng Chéng Dá
     7  * @create 2017-03-03 9:26
     8  */
     9 @Service
    10 public class UserService {
    11 
    12     public void add() {
    13         System.out.println("I am UserService's add method...");
    14     }
    15 }
     1 package com.itdjx.spring.annotation.repository;
     2 
     3 import org.springframework.stereotype.Repository;
     4 
     5 /**
     6  * @author Wáng Chéng Dá
     7  * @create 2017-03-03 9:24
     8  */
     9 @Repository("userRepository")
    10 public class UserRepositoryImpl implements UserRepository {
    11 
    12     @Override
    13     public void sava() {
    14         System.out.println("I am UserRepositoryImpl's sava method...");
    15     }
    16 }
     1 package com.itdjx.spring.annotation.repository;
     2 
     3 /**
     4  * @author Wáng Chéng Dá
     5  * @create 2017-03-03 9:23
     6  */
     7 public interface UserRepository {
     8 
     9     void sava();
    10 
    11 }
     1 package com.itdjx.spring.annotation;
     2 
     3 import com.itdjx.spring.annotation.controller.UserController;
     4 import com.itdjx.spring.annotation.repository.UserRepository;
     5 import com.itdjx.spring.annotation.service.UserService;
     6 import org.springframework.context.ApplicationContext;
     7 import org.springframework.context.support.ClassPathXmlApplicationContext;
     8 
     9 /**
    10  * @author Wáng Chéng Dá
    11  * @create 2017-03-03 9:34
    12  */
    13 public class Main {
    14 
    15     public static void main(String[] args) {
    16         ApplicationContext cxt = new ClassPathXmlApplicationContext("beans-annotation.xml");
    17         TestObject testObject = (TestObject) cxt.getBean("testObject");
    18         System.out.println(testObject);
    19 
    20         UserController userController = (UserController) cxt.getBean("userController");
    21         System.out.println(userController);
    22 
    23         UserService userService = (UserService) cxt.getBean("userService");
    24         System.out.println(userService);
    25 
    26         UserRepository userRepository = (UserRepository) cxt.getBean("userRepository");
    27         System.out.println(userRepository);
    28 
    29     }
    30 }

    控制台输出:

    com.itdjx.spring.annotation.TestObject@27ba32
    com.itdjx.spring.annotation.controller.UserController@f82753
    com.itdjx.spring.annotation.service.UserService@10fe47a
    com.itdjx.spring.annotation.repository.UserRepositoryImpl@2b0582

    注意:Spring 默认的命名策略:使用非限定类名 , 第一个字母小写。也可以通过 value 属性值标识组件的名称。

    若不修改 @Repository("userRepository") 默认的 value 值 , bean 的 id 值是 userRepositoryImpl , UserRepository userRepository = (UserRepository) cxt.getBean("userRepository"); 会抛出找不到 userRepository 这个 bean 节点。

    异常:

    Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'userRepository' available
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:687)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1207)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:284)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1081)
    at com.itdjx.spring.annotation.Main.main(Main.java:26)

  • 相关阅读:
    URL编码与解码
    什么通用数据交换格式更好
    JSON(JavaScript Object Notation)
    二维码与json都是数据交换格式
    数据的存在形式
    NSData、数据结构与数据转换
    物理结构与逻辑结构
    NSKeyedArchiver : NSCoder
    The Role of View Controllers
    Content-Type与MIME
  • 原文地址:https://www.cnblogs.com/chinda/p/6495058.html
Copyright © 2011-2022 走看看