Spring註釋,散集積累

@Configuration 
傳統方式:
 
Java代碼  
<beans>    
    <bean id="orderService" class="com.acme.OrderService"/>    
        <constructor-arg ref="orderRepository"/>    
    </bean>    
    <bean id="orderRepository" class="com.acme.OrderRepository"/>    
        <constructor-arg ref="dataSource"/>    
    </bean>    
</beans>    
<beans>  
	<bean id="orderService" class="com.acme.OrderService"/>  
		<constructor-arg ref="orderRepository"/>  
	</bean>  
	<bean id="orderRepository" class="com.acme.OrderRepository"/>  
		<constructor-arg ref="dataSource"/>  
	</bean>  
</beans>  
 
 
Java代碼  
ApplicationContext ctx = new ClassPathXmlApplicationContext("application-config.xml");    
OrderService orderService = (OrderService) ctx.getBean("orderService");    
ApplicationContext ctx = new ClassPathXmlApplicationContext("application-config.xml");  
OrderService orderService = (OrderService) ctx.getBean("orderService");  
 註解方式:
 
Java代碼  
@Configuration    
public class ApplicationConfig {    
    
        public @Bean OrderService orderService() {    
                return new OrderService(orderRepository());    
        }    
    
        public @Bean OrderRepository orderRepository() {    
                return new OrderRepository(dataSource());    
        }    
    
        public @Bean DataSource dataSource() {    
                // instantiate and return an new DataSource …    
        }    
}    
@Configuration  
public class ApplicationConfig {  
  
		public @Bean OrderService orderService() {  
				return new OrderService(orderRepository());  
		}  
  
		public @Bean OrderRepository orderRepository() {  
				return new OrderRepository(dataSource());  
		}  
  
		public @Bean DataSource dataSource() {  
				// instantiate and return an new DataSource …  
		}  
}  
 
Java代碼  
JavaConfigApplicationContext ctx = new JavaConfigApplicationContext(ApplicationConfig.class);    
OrderService orderService = ctx.getBean(OrderService.class);    
JavaConfigApplicationContext ctx = new JavaConfigApplicationContext(ApplicationConfig.class);  
OrderService orderService = ctx.getBean(OrderService.class);  
 
 
@Scope("prototype")
 
Java代碼  
@Scope("prototype")    
public class PrintTask2 implements Runnable {    
        String name;    
    
        public void setName(String name) {    
                this.name = name;    
        }    
            
        @Override    
        public void run(){    
                System.out.println(name + " is running.");    
                try{    
                        Thread.sleep(5000);    
                }catch(InterruptedException e){    
                        e.printStackTrace();    
                }    
                System.out.println(name + " is running again.");    
        }    
}   
@Scope("prototype")  
public class PrintTask2 implements Runnable {  
        String name;  
  
        public void setName(String name) {  
                this.name = name;  
        }  
          
        @Override  
        public void run(){  
                System.out.println(name + " is running.");  
                try{  
                        Thread.sleep(5000);  
                }catch(InterruptedException e){  
                        e.printStackTrace();  
                }  
                System.out.println(name + " is running again.");  
        }  
} 
 
 
若是action或Service中不@Scope("prototype"),有可能報找不到xxxaction或Service的錯誤!寫上這個就表示每次請求都從新建立一個action或Service,與SINGALON對應,俗稱「多例」。
@Component 
 
Java代碼  
@Component    
@Scope("prototype")    
public class PrintTask2 implements Runnable {    
        String name;    
    
        public void setName(String name) {    
                this.name = name;    
        }    
            
        @Override    
        public void run(){    
                System.out.println(name + " is running.");    
                try{    
                        Thread.sleep(5000);    
                }catch(InterruptedException e){    
                        e.printStackTrace();    
                }    
                System.out.println(name + " is running again.");    
        }    
}  
@Component  
@Scope("prototype")  
public class PrintTask2 implements Runnable {  
        String name;  
  
        public void setName(String name) {  
                this.name = name;  
        }  
          
        @Override  
        public void run(){  
                System.out.println(name + " is running.");  
                try{  
                        Thread.sleep(5000);  
                }catch(InterruptedException e){  
                        e.printStackTrace();  
                }  
                System.out.println(name + " is running again.");  
        }  
}
把普通pojo實例化到spring容器中,至關於配置文件中的<bean id="" class=""/>
 
@Repository
 
Java代碼  
@Repository  
public class DaoImpl implements IDao{  
}  
@Repository
public class DaoImpl implements IDao{
}
 應用於Dao層,把該類註冊到Spring容器中
 
 
@Service
 
Java代碼  
@Service  
public class ServiceImpl implements IService {  
    @Autowired  
    private IDao iDao;    
}  
@Service
public class ServiceImpl implements IService {
	@Autowired
	private IDao iDao;  
}
 應用於Service層,把該類註冊到Spring容器中
 
 
@Controller
 
Java代碼  
@Controller  
public class TestController {  
    @Autowired  
    private IService iService;  
}  
@Controller
public class TestController {
    @Autowired
    private IService iService;
}
  應用於Action層,把該類註冊到Spring容器中
 
@Autowired , @Qualifier("XXX")
 
Java代碼  
@Service    
public class TestServiceImpl {    
    @Autowired    
    @Qualifier("iTestDao2")    
    private ITestDao iTestDao;    
}    
@Service  
public class TestServiceImpl {  
    @Autowired  
    @Qualifier("iTestDao2")  
    private ITestDao iTestDao;  
}  
在ITestDao接口標上@Autowired和@Qualifier註釋使得ITestDao接口存在兩個實現類的時候必須指定其中一個來注入,使用實現類首字母小寫的字符串("iTestDao2")來注入。不然能夠省略,只寫@Autowired

獲取【下載地址】 【新技術】如今最流行的java後臺框架組合java springmvc mybaits mysql oracle html5 後臺框架源碼html

相關文章
相關標籤/搜索