Spring在注解的使用(实体类扫描)
2022/6/14 23:20:39
本文主要是介绍Spring在注解的使用(实体类扫描),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
Spring在注解的使用(实体类扫描)
package com.msb.pojo; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import org.springframework.stereotype.Component; import org.springframework.stereotype.Controller; import org.springframework.stereotype.Repository; import org.springframework.stereotype.Service; import java.io.Serializable; @NoArgsConstructor @AllArgsConstructor @Data /* 一共有4个注解, @Component放在类上,用于标记,告诉spring当前类需要由容器实例化bean并放入容器中,下面三个是他的子注解 @Controller放在controller中 @Service放在service中 @Repository用于实例化持久层bean */ @Controller public class User implements Serializable { private String user; private String password; private String name; }
spring.xml注解引入配置
<?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" xmlns:p="http://www.springframework.org/schema/p" xmlns:c="http://www.springframework.org/schema/c" 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 "> <!--use-default-filters="true" 默认扫描器识别并包含 @Component @Controller @Service @Repository 四个注解 --> <!--注解使用--> <context:component-scan base-package="com.msb.pojo" use-default-filters="false"> <!--控制只扫描Controller注解--> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> <!--控制只扫描Controller注解--> <context:exclude-filter expression="org.springframework.stereotype.Controller" type="annotation"/> </context:component-scan> <!--引入外部文件--> <context:property-placeholder location="classpath:jdbc.properties" /> <bean id="DruidDataSource" class="com.alibaba.druid.pool.DruidDataSource"> <property name="username" value="jdbc_username"></property> <property name="password" value="jdbc_password"></property> <property name="driverClassName" value="jdbc_driverClass"></property> <property name="url" value="jdbc_url"></property> </bean> <bean id="dept" class="com.msb.pojo.Dept"> <property name="name" value="我是dept的"></property> </bean> <bean id="emp" class="com.msb.pojo.Emp" autowire="byName"></bean> <bean id="user" class="com.msb.pojo.User"> <property name="password" value="1111"></property> <property name="name" value="2222"></property> <property name="user" value="1111"></property> </bean> </beans>
这篇关于Spring在注解的使用(实体类扫描)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-19永别了,微服务架构!
- 2024-05-15鸿蒙生态设备数量超8亿台
- 2024-05-13TiDB + ES:转转业财系统亿级数据存储优化实践
- 2024-05-09“2024鸿蒙零基础快速实战-仿抖音App开发(ArkTS版)”实战课程已上线
- 2024-05-09聊聊如何通过arthas-tunnel-server来远程管理所有需要arthas监控的应用
- 2024-05-09log4j2这么配就对了
- 2024-05-09nginx修改Content-Type
- 2024-05-09Redis多数据源,看这篇就够了
- 2024-05-09Google Chrome驱动程序 124.0.6367.62(正式版本)去哪下载?
- 2024-05-09有没有大佬知道这种数据应该怎么抓取呀?