FEATURED · 精选文章

Spring的注解开发

发布时间 / 2026/8/4 6:16:17
来源 / 创域科博编辑部
栏目 / 资讯中心
Spring的注解开发 一、原始注解UserDaoImpl类package com.Itheima.Dao.impl; import com.Itheima.Dao.UserDao; import org.springframework.stereotype.Component; import org.springframework.stereotype.Repository; // bean iduserDao classcom.Itheima.Dao.impl.userDaoImpl /bean Repository(userDao) public class UserDaoImpl implements UserDao { // private dateSource Override public void save() { System.out.println( save running.... ); } }ServiceImpl类package com.Itheima.Service.Impl; import com.Itheima.Dao.UserDao; import com.Itheima.Service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Component; import org.springframework.stereotype.Service; import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; import javax.annotation.Resource; // bean iduserService classcom.Itheima.Service.Impl.userServiceImpl // property nameuserDao refuserDao/property // /bean Service(userService) Scope(singleton) public class UserServiceImpl implements UserService { // Autowired //按照数据类型从spring容器中进行匹配的 // Qualifier(userDao)//按照id的值从容器中进行匹配 此处我们的qualifier注解要结合autowired一起使用 Resource(nameuserDao)//相当于Qulifierautowired private UserDao userDao; //如果使用的是xml配置的方式 set方法需要写 如果使用的是注解开发 set方法可以不写 // public void setUserDao(UserDao userDao) { // this.userDao userDao; // } Override public void save() { userDao.save(); } PostConstruct public void init(){ System.out.println(Service对象的初始化方法 userDao); } PreDestroy public void destory(){ System.out.println( Service对象的销毁方法 ); } }UserController类package com.Itheima.web; import com.Itheima.Service.UserService; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class UserController { public static void main(String[] args) { // ApplicationContext app new ClassPathXmlApplicationContext(applicationContext.xml); // UserService userService (UserService) app.getBean(userService); // userService.save(); ClassPathXmlApplicationContext app new ClassPathXmlApplicationContext(applicationContext.xml); UserService userService (UserService) app.getBean(userService); userService.save(); app.close(); } }applicationContext.xml?xml version1.0 encodingUTF-8? beans xmlnshttp://www.springframework.org/schema/beans xmlns:xsihttp://www.w3.org/2001/XMLSchema-instance xmlns:contexthttp://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 import resourceclasspath:spring-mvc.xml/ !-- 配置组件扫描-- context:component-scan base-packagecom.Itheima/ !-- 加载外部的properties文件-- context:property-placeholder locationclasspath:jdbc.properties/ !-- 在这里配置bean -- bean iddataSource classcom.alibaba.druid.pool.DruidDataSource property namedriverClassName value${jdbc.driver}/property property nameurl value${jdbc.url}/property property nameusername value${jdbc.username}/property property namepassword value${jdbc.password}/property /bean !-- bean iduserDao classcom.Itheima.Dao.impl.userDaoImpl /bean-- !-- bean iduserService classcom.Itheima.Service.Impl.userServiceImpl-- !-- property nameuserDao refuserDao/property-- !-- /bean-- /beans二、 新注解SpringConfigurtion 主配置文件类package com.Itheima.config; import com.alibaba.druid.pool.DruidDataSource; import com.mchange.v2.c3p0.ComboPooledDataSource; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.*; import javax.sql.DataSource; //该注解表示这个类是Spring的核心配置文件类 Configuration //context:component-scan base-packagecom.Itheima/ ComponentScan(com.Itheima) //import resourceclasspath:spring-mvc.xml/加载分的配置文件 Import({DataSourceConfiguration.class,XXX.class})//本质是一个数组 public class SpringConfigurtion{ }DataSourceConfiguration类 数据源相关的配置类package com.Itheima.config; import com.mchange.v2.c3p0.ComboPooledDataSource; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.PropertySource; import javax.sql.DataSource; //相当于 !-- 加载外部的properties文件-- // context:property-placeholder locationclasspath:jdbc.properties/ PropertySource(classpath:jdbc.properties) public class DataSourceConfiguration { Value(${jdbc.driver}) private String driver; Value(${jdbc.url}) private String url; Value(${jdbc.username}) private String username; Value(${jdbc.password}) private String password; Bean(dataSource)//spring会将当前方法的返回值以指定名称存储到spring容器当中 public DataSource getDataSource()throws Exception{ ComboPooledDataSource ds new ComboPooledDataSource(); ds.setDriverClass(driver); ds.setJdbcUrl(url); ds.setUser(username); ds.setPassword(password); return ds; } }package com.Itheima.web; import com.Itheima.Service.UserService; import com.Itheima.config.SpringConfigurtion; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class UserController { public static void main(String[] args) { ApplicationContext app new AnnotationConfigApplicationContext(SpringConfigurtion.class); UserService userService (UserService) app.getBean(userService); userService.save(); } }
RELATED — 相关阅读

相关资讯

LATEST — 最新资讯

最新发布

TODAY — 本日精选

新闻

WEEKLY — 本周精选

新闻

MONTHLY — 本月精选

新闻