@Component:普通
@Service:业务逻辑层
@Controller:controller层
@Repository:dao层
简化xml方式开发,只需要注解就可以完成在配置文件中的配置
组件扫描
package com.atguigu.spring5.ComponentScan;import org.springframework.stereotype.Component;/*** @BelongsProject: 02-Spring* @BelongsPackage: com.atguigu.spring5* @Author: dengLiMei* @CreateTime: 2023-02-04 16:56* @Description: TODO* @Version: 1.0*/
//value可以不写,如果不写默认是类名的首字母小写
@Component(value = "user")
public class User {public void add() {System.out.println("aaaa");}
}
package com.atguigu.spring5.ComponentScan;import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;/*** @BelongsProject: 02-Spring* @BelongsPackage: com.atguigu.spring5.ComponentScan* @Author: dengLiMei* @CreateTime: 2023-02-10 15:12* @Description: TODO* @Version: 1.0*/
public class Main {public static void main(String[] args) {ApplicationContext content= new ClassPathXmlApplicationContext("beans.xml");User user = content.getBean("user", User.class);user.add();}
}
-filtertype="" expression=""/>
@Autowired:根据类型注入
@Qualifier:根据名称进行注入,和Autowired
@Resource:可以根据类型或名称注入
@Value:普通属性注入
javax:java扩展包
Configuration:作为配置类,替代xml配置文件
package com.atguigu.spring5.SpringConfiguration;import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;@Configuration
@ComponentScan(basePackages = {"com.atguigu.spring5"})
public class SpringConfigure {}
package com.atguigu.spring5.ScopeTest;import org.springframework.stereotype.Component;/*** @BelongsProject: 02-Spring* @BelongsPackage: com.atguigu.spring5.Book* @Author: dengLiMei* @CreateTime: 2023-02-08 19:28* @Description: TODO* @Version: 1.0*/
@Component
public class Book {private String bname;private String bauthor;public void setBname(String bname) {this.bname = bname;}public void setBauthor(String bauthor) {this.bauthor = bauthor;}public static void main(String[] args) {Book book = new Book();book.setBname("abc");}public void testDemo() {System.out.println(bname + "---" + "aaa");}}
package com.atguigu.spring5.SpringConfiguration;import com.atguigu.spring5.ScopeTest.Book;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;/*** @BelongsProject: 02-Spring* @BelongsPackage: com.atguigu.spring5.SpringConfiguration* @Author: dengLiMei* @CreateTime: 2023-02-11 09:38* @Description: TODO* @Version: 1.0*/
public class Main {public static void main(String[] args) {
// 加载配置类ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfigure.class);
// 创建实例Book book = context.getBean("book", Book.class);
// 调用方法book.testDemo();}
}
输出结果:
Spring——是什么?作用?内容?用到的设计模式?
Spring——Bean管理-xml方式进行属性注入
Spring——Bean管理-注解方式进行属性注入
Spring——什么是IOC?
Spring——AOP是什么?如何使用?
Spring——什么是事务?传播行为?事务隔离级别有哪些?
Spring——整合junit4、junit5使用方法