商业网站建设所用软件,上海网站开发多少钱,建设部科技中心网站,网站按钮设计成什么颜色原因spring注解spring 原始注解1.1 Component注解1.2 Controller,Service,Repository同上1.3 注解方式依赖注入spring 新注解1. 用来解析配置类#xff0c;利用配置类替代xml注解代替了xml的繁琐配置
spring 原始注解 1.1 Component注解
!--spring 使用注解创建对象 compone…
spring注解spring 原始注解1.1 Component注解1.2 Controller,Service,Repository同上1.3 注解方式依赖注入spring 新注解1. 用来解析配置类利用配置类替代xml注解代替了xml的繁琐配置
spring 原始注解 1.1 Component注解
!--spring 使用注解创建对象 component-scan 组件扫描器 用来扫描包下面的注解 --
context:component-scan base-packagecom.lovely/利用ClassPathXmlApplicationContext可创建对象
package com.lovely.test;
Component(app)
public Class App {// coding...
}public static void main(String[] args) {ClassPathXmlApplicationContext application new ClassPathXmlApplicationContext(classpath:applicationContext.xml);App app (App) application.getBean(app);// sout(app);
}1.2 Controller,Service,Repository同上
1.3 注解方式依赖注入 // 利用注解进行 属性注入// 1. Autowired // 根据类型/* 2. Autowired // 根据类型 进行依赖注入Qualifier(userDao) // 与autowired根据名称进行依赖注入*/Resource // 3. 按照名称注入private UserDao userDao;/*public void setUserDao(UserDao userDao) {// bean 的 依赖注入 配置xml property 通过setthis.userDao userDao;}*/// 4. 对普通值注入 组件扫描器 扫描spring容器中得key 拿valueValue(${jdbc.driver})private String driver;Value(a simple string...)private String str;spring 新注解
1. 用来解析配置类利用配置类替代xml 具体使用
package com.lovely.config;import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;/*** author echo lovely* date 2020/7/22 16:59* 用于替代applicationContext.xml的配置核心类*/Configuration // 标志该类是spring的核心配置类 替换xml
ComponentScan(com.lovely) // 扫描com.lovely包下的spring注解 替换context:component-scan base-packagecom.lovely/
Import(DataSourceConfiguration.class) // 引入 DataSourceConfiguration 配置类
public class SpringConfiguration {}以数据源解析为例子
/*** author echo lovely* date 2020/7/22 17:03* 数据源配置类*/
// 加载配置文件 相当于 property-placeholder
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.userPassword})private String password;Bean(druidDataSource) // spring 会将当前方法的返回值以指定名称(druidDataSource) 储存到spring容器中public DataSource getDataSource() {DruidDataSource dataSource new DruidDataSource();dataSource.setDriverClassName(driver);dataSource.setUrl(url);dataSource.setUsername(username);dataSource.setPassword(password);System.out.println(dataSource);return dataSource;}}
测试demo
package com.lovely.web;import com.lovely.config.SpringConfiguration;
import com.lovely.service.UserService;
// import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
// import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;/*** author echo lovely* date 2020/7/21 16:15*/// 在web层上实例化bean
Controller(userController)
public class UserController {public static void main(String[] args) {/*ClassPathXmlApplicationContext app new ClassPathXmlApplicationContext(classpath:applicationContext.xml);*/AnnotationConfigApplicationContext app new AnnotationConfigApplicationContext(SpringConfiguration.class);UserService user (UserService) app.getBean(userService);user.service();/*Object obj app.getBean(druidDataSource);System.out.println(obj);*/app.close();}}