您的位置:首页 > 编程语言 > Java开发

Spring学习--通过注解配置 Bean (一)

2017-03-03 10:35 344 查看
在 classpath 中扫描组件:

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

特定组件包括:

@Component:基本注解 , 标识了一个受 Spring 管理的组件。

@Repository:标识持久层组件。

@Service:标识服务层(业务层)组件。

@Controller:标识表现层组件。

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

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
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"> 
<!-- 指定 Spring IOC 容器扫描的包-->
<context:component-scan base-package="com.itdjx.spring.annotation">

</context:component-scan>

</beans>


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

package com.itdjx.spring.annotation;

import org.springframework.stereotype.Component;

/**
* @author Wáng Chéng Dá
* @create 2017-03-03 8:44
*/
@Component
public class TestObject {
}


package com.itdjx.spring.annotation.controller;

import org.springframework.stereotype.Controller;

/**
* @author Wáng Chéng Dá
* @create 2017-03-03 9:28
*/
@Controller
public class UserController {

public void execute() {
System.out.println("I am UserController's execute method...");
}
}


package com.itdjx.spring.annotation.service;

import org.springframework.stereotype.Service;

/**
* @author Wáng Chéng Dá
* @create 2017-03-03 9:26
*/
@Service
public class UserService {

public void add() {
System.out.println("I am UserService's add method...");
}
}


package com.itdjx.spring.annotation.repository;

import org.springframework.stereotype.Repository;

/**
* @author Wáng Chéng Dá
* @create 2017-03-03 9:24
*/
@Repository("userRepository")
public class UserRepositoryImpl implements UserRepository {

@Override
public void sava() {
System.out.println("I am UserRepositoryImpl's sava method...");
}
}


package com.itdjx.spring.annotation.repository;

/**
* @author Wáng Chéng Dá
* @create 2017-03-03 9:23
*/
public interface UserRepository {

void sava();

}


package com.itdjx.spring.annotation;

import com.itdjx.spring.annotation.controller.UserController;
import com.itdjx.spring.annotation.repository.UserRepository;
import com.itdjx.spring.annotation.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
* @author Wáng Chéng Dá
* @create 2017-03-03 9:34
*/
public class Main {

public static void main(String[] args) {
ApplicationContext cxt = new ClassPathXmlApplicationContext("beans-annotation.xml");
TestObject testObject = (TestObject) cxt.getBean("testObject");
System.out.println(testObject);

UserController userController = (UserController) cxt.getBean("userController");
System.out.println(userController);

UserService userService = (UserService) cxt.getBean("userService");
System.out.println(userService);

UserRepository userRepository = (UserRepository) cxt.getBean("userRepository");
System.out.println(userRepository);

}
}


控制台输出:

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)

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: