当前位置: 首页 > wzjs >正文

阿里云建站百度收录吗最新重大新闻

阿里云建站百度收录吗,最新重大新闻,聊城开发网站建设,论坛网站在线生成背景 spring框架的一个核心功能是IOC,就是将Bean初始化加载到容器中,Bean是如何加载到容器的,可以使用spring注解方式或者spring XML配置方式。 spring注解方式直接对项目中的类进行注解,减少了配置文件内容,更加便于…

背景

spring框架的一个核心功能是IOC,就是将Bean初始化加载到容器中,Bean是如何加载到容器的,可以使用spring注解方式或者spring XML配置方式。

spring注解方式直接对项目中的类进行注解,减少了配置文件内容,更加便于管理,使用注解可以大大提高了开发效率!

注解

@Component表示一个带注释的类是一个“组件”,成为Spring管理的Bean。当使用基于注解的配置和类路径扫描时,这些类被视为自动检测的候选对象。同时@Component还是一个元注解。

@Controller组合注解(组合了@Component注解),应用在MVC层(控制层),DispatcherServlet会自动扫描注解了此注解的类,然后将web请求映射到注解了@RequestMapping的方法上。

@Service组合注解(组合了@Component注解),应用在service层(业务逻辑层)。

@Reponsitory组合注解(组合了@Component注解),应用在dao层(数据访问层)。

测试代码:

被@Component注解的类UserDaoImpl:

package com.annotation.dao.impl;import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;import com.annotation.dao.interfaces.InterfaceUserDao;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.PreDestroy;/*** @copyright 2003-2025* @author    qiao wei* @date      2025-03-24* @version   1.0* @brief     注解方式标注Bean,命名为“userDaoDao”,如果没有命名,则默认为类名首字母小写,即“userDaoImpl”。* @history   name*            date*            brief*/
@Component(value = "userDao01")
@Scope(value = "prototype")
@Lazy(value = false)
public class UserDaoImpl implements InterfaceUserDao {public UserDaoImpl() {System.out.println("类的构造方法");}@Overridepublic void print() {System.out.println("UserDaoImpl类实例,继承接口InterfaceUserDao");}@PostConstructpublic void init() {System.out.println("UserDaoImpl类init方法,在构造方法之后调用");}@PreDestroypublic void destroy() {System.out.println("UserDaoImpl类destroy方法,在Bean销毁之前调用");}
}

注解类实现的接口InterfaceUserDao :

package com.annotation.dao.interfaces;/*** @copyright 2003-2024* @author    qiao wei* @date      2024-12-22* @version   1.0* @brief     * @history   name*            date*            brief*/
public interface InterfaceUserDao {void print();
}

测试验证:

package com.annotation.dao.impl;import org.junit.jupiter.api.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;import com.annotation.dao.interfaces.InterfaceUserDao;/*** @copyright 2003-2025* @author    qiao wei* @date      2025-04-12* @version   1.0* @brief     * @history   name*            date*            brief*/
class UserDaoImplTest {@Testpublic void test01() {ClassPathXmlApplicationContext context =new ClassPathXmlApplicationContext("./xml/annotation/applicationContext.xml");InterfaceUserDao userDao = context.getBean("userDao01", InterfaceUserDao.class);userDao.print();context.close();}
}

读取的配置文件:

<?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:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-4.0.xsd"><!--  进行注解扫描。扫描制定的包及其子包。  --><context:component-scan base-package="com.annotation" />
</beans>

运行结果:

http://www.dtcms.com/wzjs/556749.html

相关文章:

  • 胡芦娃app软件下载网站有名的网站建设
  • 外贸建站 厦门网络推广工作能长久吗
  • 电子商务网站建设的一般步骤有贵阳做网络推广的公司
  • 怎么上传网站源码厦门市建设工程造价网站
  • 平台与网站有什么区别wordpress修改为中文
  • 3m网站源码设计网站多少钱
  • 网站建设软件排行企业网站建设包含哪些内容
  • 烟台外贸网站建设公司总经理培训推广哪家好
  • 网站建设word文档wordpress获取qq头像评论
  • 做图片祝福的网站建设部工程业绩网站
  • 最专业的礼品网站案例参考wordpress 手机首页
  • 安全质量报监建设局网站wordpress和vue
  • 网站修改域名qt开发安卓app
  • 长春网站制作系统买网站平台名字吗
  • 网站建设需要提供什么资料网站开发如何建设公共页面
  • 搭建企业网站流程河南旅游网站建设
  • 杭州网站建设市场wordpress多用途主题排行
  • 专业网站排名优化小程序appld
  • 网站建设中的注册和登录页面网页传奇哪个最火
  • 妇科医院网站设计高端酒店网站模板
  • 哪里有网站制作价格开发外贸产品的网站
  • 大连网站建设报价优质商家用asp.net开发网站的优势
  • 信息门户网站建设报价沈阳网站优化推广方案
  • 做网站补贴网站建设的原则
  • 营销型网站建设方案书欧米茄表价格官网报价
  • 网站建设速度如何解决购物网站设计会员管理模块
  • 电子商务网站建设重要性百度怎么自己做网站吗
  • 网站优化的好处建立网站站点的过程
  • 哪个网站可以做ppt赚钱济南网络品牌推广
  • 国外有没有专门做靶材的网站网站文案优化