本文主要是介绍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在注解的使用(实体类扫描)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!