Spring的注解开发

一、原始注解

UserDaoImpl类

复制代码
package com.Itheima.Dao.impl;

import com.Itheima.Dao.UserDao;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;


//   <bean id="userDao" class="com.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 id="userService" class="com.Itheima.Service.Impl.userServiceImpl">
//        <property name="userDao" ref="userDao"></property>
//    </bean>

@Service("userService")
@Scope("singleton")
public class UserServiceImpl implements UserService {

//    @Autowired  //按照数据类型从spring容器中进行匹配的
//    @Qualifier("userDao")//按照id的值从容器中进行匹配 此处我们的qualifier注解要结合autowired一起使用
    @Resource(name="userDao")//相当于Qulifier+autowired
    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 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"
       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 resource="classpath:spring-mvc.xml"/>
<!--    配置组件扫描-->
    <context:component-scan base-package="com.Itheima"/>



<!--    加载外部的properties文件-->
    <context:property-placeholder location="classpath:jdbc.properties"/>
    <!-- 在这里配置bean -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
    <property name="driverClassName" value="${jdbc.driver}"></property>
    <property name="url" value="${jdbc.url}"></property>
    <property name="username" value="${jdbc.username}"></property>
    <property name="password" value="${jdbc.password}"></property>
</bean>

<!--    <bean id="userDao" class="com.Itheima.Dao.impl.userDaoImpl">   </bean>-->

<!--    <bean id="userService" class="com.Itheima.Service.Impl.userServiceImpl">-->
<!--        <property name="userDao" ref="userDao"></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-package="com.Itheima"/>
@ComponentScan("com.Itheima")

//<import resource="classpath: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 location="classpath: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();

    }
}
相关推荐
旺仔Sec11 小时前
2026年江西省职业院校技能大赛(中职组)应用软件系统开发赛项竞赛样题
java·应用软件系统开发
Dovis(誓平步青云)12 小时前
几个方案来回选不定?做一个随时切换的候选推荐页
android·java·服务器·开发语言·javascript·数据库·智能化
一条破秋裤12 小时前
Linux 线程同步:读写锁
java·linux·jvm
蜗牛互联网12 小时前
从Barclays扩大Claude部署看受监管软件工程的责任重分配
java·大数据·人工智能·后端·软件工程
天远API12 小时前
零信任架构实战:基于天远股权穿透构建自动化供应链授信穿透网关
java·人工智能·架构·自动化
最强小杰12 小时前
claude-opus-5.5 API 接入教程:anthropic-version 头、tools 定义、流式事件格式三个常见问题全部梳理,收藏备用
java·服务器·网络·ai
Cc.Y12 小时前
Java零基础入门:方法(函数)深度掌握
java·开发语言
奋发向前wcx13 小时前
CSP-J复赛模拟赛1 王晨旭补题 2026.10.2
java·开发语言
卓怡学长13 小时前
w192基于springboot“考研情报站”微信小程序设计与实现
java·spring boot·spring·微信小程序·intellij-idea
周杰伦fans13 小时前
C#对话摘要降低Token消耗
人工智能·后端·c#