山东省建设厅继续教育网站,泰安做网站公司哪家好,网站建设方案设计心得,wordpress子主题缺点Bean基本注解开发
Spring除了xml配置文件进行配置之外#xff0c;还可以使用注解方式进行配置#xff0c;注解方式慢慢成为xml配置的替代方案。我们有了xml开发的经验#xff0c;学习注解开发就会方便很多#xff0c;注解开发更加快捷方便。Spring提供的注解有三个版本 2.…Bean基本注解开发
Spring除了xml配置文件进行配置之外还可以使用注解方式进行配置注解方式慢慢成为xml配置的替代方案。我们有了xml开发的经验学习注解开发就会方便很多注解开发更加快捷方便。Spring提供的注解有三个版本 2.0时代Spring开始出现注解2.5时代Spring的bean配置可以使用注解完成3.0时代Spring其它配置也可以通过注解完成我们进入全注解时代SpringBoot基本的Bean注解主要是使用注解的方式替代原有的xml的bean标签及其标签属性的配置使用Component注解替代bean标签 被该注解标识的类会在指定扫描范围内被Spring加载并实例化具体实例代码 使用注解将目标类注册为bean对象 package com.example.DAO.Impl;import com.example.DAO.UserDAO;
import org.springframework.stereotype.Component;Component(value userDAO) // 设置bean对象的名称
public class UserDAOImpl implements UserDAO {
}在配置文件中设置注解组件扫描范围识别Component注解 ?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:contexthttp://www.springframework.org/schema/contextxsi:schemaLocationhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttps://www.springframework.org/schema/context/spring-context.xsdcontext:component-scan base-packagecom.example.DAO/
/beans测试代码获取bean对象 package com.example.Test;import org.springframework.context.support.ClassPathXmlApplicationContext;public class TestApplicationContext {public static void main(String[] args) {ClassPathXmlApplicationContext context new ClassPathXmlApplicationContext(application.xml);Object userDAO context.getBean(userDAO);System.out.println(userDAO);}
} 运行结果如下