我们使用注解前要先进行导入这个包 就是这里面的一个aop包。
写配置文件,我们使用这种注解头文件,开启注解,同时我们有添加了一个标签,组件扫描让其这个包下的所有组件都生效
<?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">
<!-- 组件扫描-->
<context:component-scan base-package="aw"/>
<!-- 开启注解-->
<context:annotation-config/>
</beans>
@Component组件用于pojo层
@Repository组件用于dao层
@Service组件用于service层
@Controller组件用于controle(servlet)层
这些注解的意思是都将他们注册到spring中。
我们以Component为例
@Component等价与 <bean id=“user” class=“aw.pojo.User”
@Value等价与<property name=“name” value=“阿威”/
package aw.pojo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class User {
@Value("阿威")
private String name;
public void show(){
System.out.println(name+"1111");
}
}
同时我们还可以使用注解添加他的作用域。
package aw.pojo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.web.context.annotation.RequestScope;
@Component
@Scope("singleton")
public class User {
@Value("阿威")
private String name;
public void show(){
System.out.println(name+"1111");
}
}
xml是属于万能使用,复杂的使用。
注解只适用于简单的,当自己的类。
java开发
首先是我们创建一个类,这里我们的注解我们已经了解过了。
package aw.pojo.java;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
@Component
@Scope("singleton")
public class User {
@Value("阿威")
private String name;
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
'}';
}
}
现在我们不是写applicationContext.xml配置文件,而是写一个配置类,我们首先是写一个
@Configuration配置注解,他的作用相当于.xml,同时也将这个类注册到spring中。
@Bean注解就相当于<bean 标签,id是方法名,class类型是返回值类型。
package aw.pojo.java;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
public class JavaConfig {
@Bean
public User getUser(){
return new User();
}
}
因为我们是通过java注解写所以要通过注解配置文件(AnnotationConfig)读取这个类,然后在通过id获取方法类型。
public class Test2 {
public static void main(String[] args) {
AnnotationConfigApplicationContext javaConfig = new AnnotationConfigApplicationContext(JavaConfig.class);
User getUser = (User) javaConfig.getBean("getUser");
System.out.println(getUser);
}
}
我们还可以在配置类中添加一些其他的注解
@ComponentScan(“aw.pojo.java”) 组件扫描 ( <context:component-scan base-package=“aw”/>)
@Import(JavaConfig2.class) 导入另一个配置文件( <import resource=""/)
@Configuration
@ComponentScan("aw.pojo.java")
@Import(JavaConfig2.class)
public class JavaConfig {
@Bean
public User getUser(){
return new User();
}
}